In other words, is there an AND operator in c++ regex? Normally I would just use | but it doesn't work
For example I want to return only 2 and 1 digit numbers
string subject("This 91 - 500abc7 is a 5 test");
regex re("\\d\\d");
This only returns 2 digit numbers, how do I add a second condition to also match single digits "\d"
Result should be:
91 - 7 - 5
It is not a "and" you want to have 1 OR 2 digits (\d|\d\d).
but regex have notation for numbered repetition: \d{1,2}
Issue is that \d{1,2} would match 50 in 500.
So you might add (negative) look ahead/behind:
(?<!\d)\d{1,2}(?!\d) (1 or 2 digits not preceded and followed by another digit)
so std::regex re(R"((?<!\d)\d{1,2}(?!\d))");
Related
Below is my regex for matching 2 digit where tens place value is 2 or 3 and it is working fine.
^(?=[2,4])\d{1,2}$
As soon as I add the regex for matching single digit in above regex , It started matching single digit and as well all 2 digit number.
^(?=\d|[2,4])\d{1,2}$
I want below sample input to be matched.
0
1
2
3
24
44
48
29
28
Below not to be matched.
99
11
33
55
77
Also It will great help if I would get to know why my regex is not working.
You get a difference in matches as the positive lookahead asserts that there must be to the right what you specify. In there first pattern that is either 2 4 or , and in the second case just a single digit.
You don't have a comma in your example data, so in that case you can match an optional 2 or 4 using just [24]? followed by a digit without any lookarounds.
^[24]?\d$
See a regex demo.
Try this: ^(\d|[2,4]\d)$
Test regex here: https://regex101.com/r/aZo7fK/1
^(\d|[2,4]\d)$
^ matches the start of string
(\d|[2,4]\d) matches either a single digit(0-9) or a two digit number which starts with either 2 or 4
$ matches the end of the string
This matches either a single digit(0-9) number or a two digit number which starts with either 2 or 4.
I suggest
^[2,4]?[0-9]$
pattern; where
^ - anchor, start of the text
[2,4]? - optional 2 or 4 digit for tens
[0-9] - mandatory digit 0..9 for units
$ - anchor, end of the text
Edit: Now, let's have a look at your current patterns; the first is
^(?=[2,4])\d{1,2}$
Here
(?=[2,4]) - look ahead for 2 or 4
\d{1,2} - one or two digits
as we can see 3 doesn't match: look ahead fails to find 2 or 4. As for your second attempt
^(?=\d|[2,4])\d{1,2}$
pattern, where
(?=\d|[2,4]) - look ahead for ANY digit (note, that |[2,4] is redundant)
\d{1,2} - one or two digits
the pattern matches too many texts; technically it matches any one or two digit numbers, e.g. for:
79
we have
(?=\d|[2,4]) - look ahead - succeeds with 7
\d{1,2} - one or two digits - succeeds with 79
Please, I need to validate Iranian postal code using regex.
I write this regex for this case \b([^02\n\D]){4}[^5](\d){5} but its not working on rule number 5 and 7.
please help me to fix it.
this is some rules about this regex:
It's all numeric
10 digit count
don't use 0 in first 5 digit
don't use 2 in postal code
First 4 digit is not the same
The 5th digit cannot be 5
all digits aren't the same
The following regex satisifes your conditions:
\b(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}\b
Click for Demo
Explanation:
\b - a word boundary
(?!(\d)\1{3}) - negative lookahead to make sure that the 1st 4 digits are not the same.
[13-9]{4} - matches 4 occurrences of all the digits except 0 and 2
[1346-9] - matches a single digit that is not a 0,2 or 5
[013-9]{5} - matches 5 occurrences of all the digits except 2
\b - a word boundary
I am trying to figure out how to use regex to pass a 6 digit number string. My trouble is the string can be any 6 digits, unless it starts with 12. So the first digit can be 1 but not if second digit is 2. The second digit can be 2, but not if the first is 1.
I tried this, ([^1])([^2])(\d{4}) but that does not take into account both digits, so it will block anything with a 2 in the second spot.
Thank you for any help.
You may use
^([02-9][0-9]|[0-9][013-9])[0-9]{4}$
See the regex demo
Details:
^ - start of string
([02-9][0-9]|[0-9][013-9]) - either of the two alternatives:
[02-9][0-9] - any digit but 1 and then any digit
| - or
[0-9][013-9] - any digit and then any digit but 2
[0-9]{4} - any 4 digits
$ - end of string.
Another way is to use a negative lookahead:
^(?!12)[0-9]{6}$
See another demo. Here, (?!12) fails the match if the first 2 digits are 12. The [0-9]{6} will match 6 digits.
Depending on the regex library/method, ^/$ anchors may not be required. Lookaheads are not always supported, too.
I want to write a regular expression on Google Form
First Character between 1 to 9
Second and Third any alphabets (Upper Case)
and next 3 characters should be number like 541 or 001 but not 000
This expression is also taking 000
[1-9][A-Z]{2}[0-9]{3}
Use alternations:
[1-9][A-Z]{2}([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9])
See regex demo
Here,
[1-9] - matches 1 digit from 1 to 9
[A-Z]{2} - two uppercase ASCII letters
([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9]) - 3 alternatives:
[1-9][0-9][0-9] - 3-digit numbers starting with 1
[0-9][1-9][0-9] - 3-digit numbers having 1 in the middle
[0-9][0-9][1-9] - 3-digit numbers ending with 1
Also, see this regex demo.
Use a negative look-ahead to avoid the triple zero at the end:
[1-9][A-Z]{2}(?!000)[0-9]{3}
Using the alternation operator
[1-9][1-9][1-9]|0[1-9][1-9]|00[1-9]|0[1-9]0
This regex does not work for me as selects all groups of two and multiple digits and not the string.
abcde9 = match
abcde12 = not matched
abcde12345678 = not matched
What I have at the moment is this, it I just can't include the 0 and the 10 as two digits numbers in the regex, can anyone help me?
\d{0,10}[1-9]
If you want to match any string containing exactly one integer from 0 to 10 then use
^\D*(\d|10)\D*$
which means "any non-digit content followed by either a single digit or the number 10 and then followed by any non-digit content"
try it at regex101
I think you are looking for
^\D*(?:[0-9]|10)(?:\D+(?:[0-9]|10))?\D*$
See demo
This will match a whole string that contains 1 or 2 whole integer numbers from 0 to 10, and no other digits.
The regex breakdown:
^ - start of string
\D* - 0 or more characters other than digit
(?:[0-9]|10) - numbers from 0 to 10
(?:\D+(?:[0-9]|10))? - 1 or 0 occurrence of
\D+ - 1 or more characters other than digit
(?:[0-9]|10) - numbers from 0 to 10
\D* - 0 or more characters other than digit
$ - end of string
Is that what you looking for:
/(0[1-9])$/
You can test that regex to make sure it fits your needs:
https://regex101.com/r/hX6lB7/3