I've found lots of questions on here on how to exclude a substring from results, but I want to exclude lines that are exact matches and simply can't figure out how to do it.
With the test data below, how would I match everything except for 11 and 111?
0
1
00
01
10
11
000
001
010
011
100
101
110
111
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
I've tried various things, such as this:
^((?!11|111).)*$
But that excludes substring matches, when again I'm wanting to exclude exact matches.
Is this possible with regex? If so, how can excluding exact matches be achieved?
You need to have the end-of-line included in the negative look ahead:
^(?!(11|111)$).*$
See live demo (using your data)
Without including the end-of-line, you are only asserting that the input doesn't start with 11 or 111, when what you want is to assert is that the entire input (start to end) isn't 11 or 111.
Through PCRE verb (*SKIP)(*F),
^(?:11|111)$(*SKIP)(*F)|.+
DEMO
OR
^(?:(?!^(?:111|11)$).)++$
DEMO
Related
I need a regular expression to extract a specific set of numbers from a string. The string could contain letters, special characters and spaces.
Input examples:
This is a test 99 12 3456
This is test 2 94123456
This is test 3 357 95123456
This is test 4 35797123 456
And so on…
The regex should look for a string of 8 numbers starting with 94 or 95 or 96 or 97 or 99 followed by 6 more numbers.
example:
94<6 more numbers here>
95<6 more numbers here>
96<6 more numbers here>
97<6 more numbers here>
99<6 more numbers here>
or 11 numbers starting with 357 followed by 94 or 95 or 96 or 97 and 6 more numbers.
example:
35794<6 more numbers here>
35795<6 more numbers here>
35796<6 more numbers here>
35797<6 more numbers here>
35799<6 more numbers here>
So the output should either be 8 numbers, or 11 numbers. Less than 8 or more than 11 is not a valid output. Also anything between 8 and 11 is not valid.
Hope this makes it more clear
Thanks for your help
Maybe this:
(357|94|95|96)[\d ]{6,}
Which means "357" or "94" or "95" or "96" followed by at least six digits and/or spaces. I wasn't sure exactly what you want. It would be better just to post the exact input and output desired.
If you’re working in an environment that supports lookbehinds, you can ensure you’re not matching a partial number by using a negative lookbehind and negative lookahead:
/(?<!\d)(?:357)?9[4-79]\d{6}(?!\d)/
(?<!\d): Negative lookbehind (ensure there isn’t a digit before the matching expression)
(?:357)?: Create a non-capturing group of 357 to attach an optional quantifier (match 357 zero to 1 times)
9: Match 9
[4-79]: Character set with range 4-7 and 9 (match one of these characters)
\d{6}: Match a digit exactly 6 times
(?!\d): Negative lookahead (ensure there isn’t a digit after the matching expression)
This regular expression will do it if you remove the spaces from the input first: 3579[4-9](?:\d{8}|\d{6})
I try to create a regular expression which skips 1 but should allow n number of 1s.
I tried using ^([^1])*)$
It skipped 1 but also skipping any 1s.
Code
See regex in use here
\b1{2,}\b
To match any number except 1, you can use the following regex:
See regex in use here
\b(?!1\b)\d+\b
Results
Input
1
11
111
1111
Output
Only matches are shown below
11
111
1111
Explanation
\b Assert position as a word boundary
1{2,} Match the digit 1 (literally) two or more times
\b Assert position as a word boundary
I am new to Stackoverflow and I need your help to match payment invoice number. So that user can't input wrong invoice number. It should match the invoice pattern like 612(fixed) 10/20/30/40/50(only one from 5 of them) 001-064(one at a time) 0000(fixed) 01-64(one at a time) 00(fixed) and then 0001-9999(allowed)
If I show you one invoice number it'll be like this one 612 30 005 0000 55 00 1234 without any space like this 61230005000055001234
I can't figure it out how could I do this. please help me if you can.
^612\s?[1-5]0\s?0(?:[0-5]\d|6[0-4])\s?0000\s?(?:[0-5]\d|6[0-4])\s?00\s?\d{4}$
Should do the job for you, assuming that spaces are optional, but in fixed position and only single ones.
^ is an anchor for the beginning of the string
612\s? matches 612 literally, followed by an optional space
[1-5]0\s? matches 1/2/3/4/5 followed by 0 and an optional space
0([0-5]\d|6[0-4])\s? means 0 followed by either 0-5 and any digit or 6
and 0-4, followed by an optional space
0000\s? matches 0000 literally, followed by an optinal space
([0-5]\d|6[0-4])\s? is either 0-5 and any digit or 6 and 0-4, followed by an optional space
00\s? matches 00 literally, followed by an optional space
\d{4} means any 4 digits
$ is an anchor for the end of the string
https://regex101.com/r/iU5jY5/3
612[1-5]00(?:[0-5][0-9]|6[0-4])0000(?:0[0-9]|[1-5][0-9]|6[0-4])00[0-9]{4}
See a demo here.
I have to generate a regular expression to detect patterns of text where credit card numbers are involved, I have a regular expression but fails when the text is altered with simple spaces between the text for example (not valid credit card number):
4320 7589 9456 0123
The regex is:
4\d{3}(\s+|-)?\d{4}(\s+|-)?\d{4}(\s+|-)?\d{4}
This regex match easy, but if someone alter the text with spaces between any number like this:
4 320 7589 9456 0123
Does not match, I need a regex to detect any possible variable with spaces, special symbols, letters, some examples:
43 20 75 89 94 56 01 23
4 3 2 0 7 5 8 9 9 4 5 6 0 1 2 3
4320a7589b9456c0123
4320$7589$9456$0123
4320_7589_9456_0123
I don't know if I can strip any space, symbols from the pattern to analyze the text?
I am posting because you actually asked for help with pattern to match any number of non-digits between the first 4 and 15 more digits.
The pattern is
^4(?:\D*\d){15}$
See demo
Regex breakdown:
^ - start of string
4 - literal 4
(?:\D*\d){15} - 15 occurrences of sequences of...
\D* - 0 or more non-digit symbols before..
\d - a digit
$ - end of string
If you need to capture, you can capture (like ^4((?:\D*\d){3})((?:\D*\d){4})((?:\D*\d){4})((?:\D*\d){4})$), but the submatches will still contain the "junk" in-between digits.
I have tried the following to only allow integers in my text box, this works great but it allows a zero in there. Is there anything else I can add to prevent a zero being added?
\d+
This will allow 10 but not 01, and it will allow only numbers consisting of digits, i.e., no periods or minus signs...but also no plus signs, scientific notation etc.
^[1-9][0-9]*$
A minor variation is this:
/\d*[1-9]\d*/
That would allow leading zeros.
If you are not concerned about negatives and silly numbers like 07, this will do:
/[1-9]\d*/
For a more robust solution, I suggest converting the matched string to integer and check if it fulfills your criteria.
Code:
^([1-9][0-9]+|[1-9])$
Example: http://regexr.com/3annd
Tested with:
0
10
01
11
00
1
100
^(0*[1-9][0-9]*)$
This will allow "silly" numbers like 007 as well, but not 0 or 000 or an empty string.
Note that \d matches also digits from other character sets like ٠١٢٣٤٥٦٧٨٩. See: \d is less efficient than [0-9].
^ denotes the start, $ the end of the string. Together they ensure that the whole string is matched.
^(\d{2}[1-9])$
matches with:
from 001 to 999
example
001
099
999
does not match
000
01
0