I am looking for a specific combination in a txt file that contains multiple lines (Notepad ++). The structure of a line I am looking for is as follows:
xxxxxx N N -1 -1 -1 N (end line)
So I first have an identifier of 6 or more characters, followed by 6 numbers (N) spaced by a tab. N can be values 1, 0 or -1.
I am looking for those lines that contain '-1' in position 3, 4 and 5. The other positions can take any of the 3 values.
I have searched online and applied searches such as:
\t-?\t-?\t-1\t-1\t-1\t-?
\t?.\t?.\t-1\t-1\t-1\t?.
t?.\t?.\t-1\t-1\t-1\t?.\n
\t-1\t-1\t-1\t?.\n
Yet, the last N in the line is not taken into account, so that if its value is 0 for example, that line will not be selected.
What is the way to write this search? I understand Notepad ++ is written in C++.
Can you try to follow this pattern?:
^([a-zA-Z0-9]{6,})\s*(-1|0|1)\s*(-1|0|1)\s*((-1\s*?){3})\s*(-1|0|1)\s?
https://regex101.com/r/yM5xD3/2
Explanation:
^: Start of the line.
([a-zA-Z0-9]{6,}): Any character six or more times.
\s*: space/tab/newLine zero o more times.
(-1|0|1): One of those numbers.
\s*: ...
(-1|0|1): One of those numbers.
((-1\s*?){3}): -1 one time followed by space/tab/newLine zero or more times. (The '?' means that the regex will try to get the less amount of \s as possible)
\s*: ..
(-1|0|1): ...
And the last \s?: looks for zero or one Space/tab/newLineCharacter
You can try the following regex:
^[a-zA-Z0-9]+\t(-1|0|1)\t(-1|0|1)\t[\-][1]\t[\-][1]\t[\-][1]\t(-1|0|1)$
I tried on the following sample and it worked for me.
xxxxxx 1 1 -1 -1 -1 1
xxxxxx 0 1 -1 -1 -1 0
test12 -1 1 -1 1 -1 0
xxxxxx 1 1 -1 -1 -1 0
test13 0 1 -1 -1 1 -1
Hope it helps.
Related
Hy, just a quick one here, does anyone know a good regular expression for a percentage and another number? I want to use it in a XML Schema..
Should Match:
-1
100.00
20.00
20.0
10.0
20
99.0
66.4
0.00
So it should match a percentage OR -1
My approach doesnt work...
([/-1]{1}|\d{1,3}\.\d{1,2})
Thanks!
(-1\n|\b(100|\d{1,2})(\n|(\.\d{1,2})))
Explanation:
(-1\n| // when not percentage OR ...
\b // word boundary - must not be other symbols in front
(100| // when integer part is equal to 100 OR ...
\d{1,2} // when integer part is number between 0 and 99
) // then after integer part must follow:
(\n| // new line symbol OR ...
(\.\d{1,2}) // dot symbol AND fractional part composed of 0 and 99
)
)
For regular expressions I usually use and suggest MDN as a reference.
That being said if I understand what you are trying to do this would work for you:
/(?=\s+|^)(?:-1|100(?:\.0+)?|\d{1,2}(?:\.\d{1,})?)(?=\s+)/gm
This would match strings that have nothing or white-spaces before and after
(?=\s+|^) content (?=\s+)
You can optionally alter that to ^(?: content)$ if you want each number to be the only thing on each line.
Where content is any of:
-1 ( -1 )
100 optionally folowed by "." and 1 or more 0s ( 100(?:\.0+)? )
1 or 2 digits optionally followed by "." and 1 or more decimals ( \d{1,2}(?:\.\d{1,})? )
You could alter the ending of {1,} to {1,X} where X is the max number of decimals you want to match.
For matching results check RegExr
I hope you can help me, I'm studying how to make a regex and now I have this problem:
Write a regex that accepts strings with 0 and 1 and that has a 1 on position 5 from right to left.
e.g. 10000 is accepted because it has an 1 on the position 5 from right to left or 010000, 0010000 or 1110000 are accepted.
I was thinking with something like: (0+1)*+1(0+1)(0+1)(0+1)(0+1)(0+1)
You can use this regex:
1[01]{4}$
If you want to match full input then use:
^[01]*1[01]{4}$
Here 1[01]{4}$ ensures that we have 4 digits of 0 and 1 after we match 1 thus making 1 at 5th position from right to left.
RegEx Demo
Well - think of it this way. It needs to be as many 1s and 0s as you please, followed by a 1, followed by 4 more ones or zeroes.
So:
my_regex =
"^[01]*" + // Starts with One or zero, zero or more times
"1" + // Followed by a one
"[01]{4}$" // Followed by four things, which could be either zero or one, before ending.
Your (0 + 1) syntax looks foreign to me. I'm using character classes to specify the [01] things but you could use (0|1) in their place, which is what your attempt looks more like.
The full thing, together, is ^[01]*1[01]{4}$
VB2010 Using regex I cant seem to get this seemingly easy regex to work. I first look for a line with a keyword TRIPS that has my data and then from that line I want to extract repeated groups of data made up of an alpha code and then a number.
MODES 1 0 0
OVERH X 28 H 0 Z 198
TRIPS X 23 D 1 Z 198
ITEMSQ 1 0 0
COSTU P 16 E 180
CALLS 0 0
I have
^TRIPS (?<grp>[A-Z]\s{1,4}\d{1,3})
Which gives me one match and the first group "X 23". So I extend it by allowing it to match up to 4 groups.
^TRIPS (?<grp>[A-Z]\s{1,4}\d{1,3}){0,4}
but I get one match with still only one group.
You aren't allowing for white space between the groups. You need to do something like this:
^TRIPS ((?<grp>[A-Z]\s{1,4}\d{1,3})\s+){0,4}
I have a set of age data, like below;
1
2
3
4
5
6
7
8
9
10
1,1
1,2
1,3
2,12
11,13,15
7,8,12
12,15
14,16,17
15,6
13,11,10,2
And so on... I am trying to use Regex in to target a 'mixed' range of childrens ages. The logic requires at least a combination of 2 childen (so requires one of the lines with a comma), with at least one aged under 10 (min is 1), and at least one aged equal or greater to 10 (max 17).
My expected results from the above would be to return these lines below, and nothing else;
2,12
7,8,12
15,6
13,11,10,2
Any advice would be appreciated on how to resolve? Thanks in advance, I am continuing to try to correct.
You can use this regex to meet your requirements:
^(?=.*\b[1-9]\b)(?=.*\b1[0-7]\b)[0-9]+(?:,[0-9]+)+$
RegEx Demo
There are 2 lookaheads to assert 2 numbers one between 1-9 and another between 10-17
([1-9]) matches a number that should be between 1 and 9
1[0-7] matches a number that should be between 10 and 17
[0-9]+(?:,[0-9]+)+ in the regex is for matching 1 or more comma separated numbers in the middle.
You can do it with
\b\d,1[0-7]\b
provided the ages always are sorted (youngest to oldest).
If the age of 0 isn't allowed, change to
\b[1-9],1[0-7]\b
It checks for a single digit followed by a comma and one followed by a single digit in the range 0-7.
See it here at regex101.
I am trying to accept only 7 digit long number not starting with 0 or 1
7089097 - OK
0089097 - Not good
1089097 - Not good
This is what i tried:
^\[2-9][0-9]{7}$
And not working:)
This regex will work:
^[2-9][0-9]{6}$
Out of 7 digits 1 is consumed by first position 2-9 and then next 6 digits can be from 0-9
You can try this:
^[2-9][0-9]{6}$