I need help with the below regex:
(0 ?)([1-8] ?){1}(\d ?){9}|(\d ?){10}
The regex should match either 10 or 11 digits
The first digit should be 0
The second digit should contain from 1 - 8 digits only
All digits can have spaces in between e.g. 0 1 2 7 4 3 3 3 4 4 4
Digits can be without spaces e.g. 01274333444
The regex I created works for most of the scenarios apart from the third condition i.e. The second digit should contain from 1 - 8 digits only.
Any help very much appreciated
Here is what your regex (0 ?)([1-8] ?){1}(\d ?){9}|(\d ?){10} is currently doing:
It's also trying to match 11 or 12 digits instead of 10 or 11.
What you need to do is change it one of the following ways:
Add a group around the OR | to limit it's scope: (0 ?)([1-8] ?){1}(?:(\d ?){8}|(\d ?){9})
Or preferably change it to a 8 or 9 character match: (0 ?)([1-8] ?){1}(\d ?){8,9}
You can use this:
^0 ?[1-8] ?(?:[0-9] ?){8,9}$
I used anchors ^ and $ to ensure that there is no leading or trailing digits.
Try this one:
^(0\s?)([1-8]\s?)(\d\s?){8,9}$
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
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))");
I have a regular expression like:
/^([0-9]{2,3})/
This will accepts 2 or 3 number digits between 0 and 9
123 or 12
I need a validate to: if the number has 3 digits, the first should be 0, in our case 023
and if not, number should be the 2 digits one: 12
Can anyone help me?
You may use
^0?\d{2}$
See the regex demo
Details
^ - start of string
0? - an optional 0
\d{2} - two digits
$ - end of string
How I can write a regex which accepts 10 or 14 digits separated by a single space in groups of 1,2 or 3 digits?
examples:
123 45 6 789 1 is valid
1234 567 8 9 1 is not valid (group of 4 digits)
123 45 6 789 109 123 8374 is not valid (not 10 or 14 digits)
EDIT
This is what I have tried so far
[0-9 ]{10,14}+
But it validates also 11,12,13 numbers, and doesn't check for group of numbers
You may use this regex with lookahead assertion:
^(?=(?:\d ?){10}(?:(?:\d ?){4})?$)\d{1,3}(?: \d{1,3})+$
RegEx Demo
Here (?=...) is lookahead assertion that enforces presence of 10 or 14 digits in input.
\d{1,3}(?: \d{1,3})+ matches input with 1 to 3 digits separated by space with no space allowed at start or end.
aggtr,
You can match your use case with the following:
^(?:\d\s?){10}$|^(?:\d\s?){14}$
^ means the beginning of the string and $ means the end of the string.
(?:...) means a non-capturing group. Thus, the part before the | means a string that starts and has a non-capturing group of a decimal followed by an optional space that has exactly 10 items followed by the end of the string. By putting the | you allow for either 10 or 14 of your pattern.
Edit I missed the part of your requirement to have the digits grouped by 1, 2, or 3 digits.
I am trying to write some form validation, I need one of the inputs to be 13000-99999.
(^[1-1][3-3]?[0-9]?[0-9]?[0-9]?$|^[0-9][0-9][0-9][0-9][0-9]$)
It does not work as expected and it match all the following :
10 \\ matched but it should not
10000 \\ matched but it should not
12999
13000
20000
99999
can anyone help me? Thanks!
Although the way you are doing this is not ideal. But if you are doing with this approach, your regex needs some changes:
(^[1-1][3-9][0-9][0-9][0-9]$|^[2-9][0-9][0-9][0-9][0-9]$)
This is because if the 1st digit is a 1, then the second number should be between a 3 and a 9. If the 2nd digit is a 2, then any of 0-9 is valid for the second digit. The last three digits are always 0-9 range.
You have more than one issue here
(^[1-1][3-3]?[0-9]?[0-9]?[0-9]?$|^[0-9][0-9][0-9][0-9][0-9]$)
1 - You have to remove all question mark ?
2 - In the second pattern in the first part [3-3] should be from [3-9]
3 - In the second part after | this should be from [2-9]
You can use one of the following regex (^[1-1][3-9][0-9][0-9][0-9]$|^[2-9][0-9][0-9][0-9][0-9]$), (^[1-1][3-9]|^[2-9][0-9])[0-9][0-9][0-9]$ or 1[3-9]\d{3}|[2-9]\d{4}
first you need to match any number that start from 1 and from 3 to 9 or start from 2 to 9 and any number 0 to 9 and all rest number can any from 0 to 9