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
Related
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))");
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 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
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
I have problem with one regular expression to check if a (french) phone number is correct.
Phone number must start with one 0 continue with one 1 or 2 or 3 or 4 or 5 or 9 and continue with 8 numbers but theses numbers must not be the same like 00000000 or 11111111...
My current regular expression :
/^0(1|2|3|4|5|9){1,1}[0-9]{8,8}/i
Thanks in advance for help.
You can use the following regex:
/^0[1-59](?!(\d)\1{7}$)\d{8}$/i
Some points:
{1,1} is as good as being removed.
{8,8} is as good as {8}.
(1|2|3|4..) can be replaced with character class - [1234]
[12345] can be replaced by range in a character class - [1-5]
The above regex uses negative look-ahead assertion - (?!(\d)\1{7}) to assert that the 8 digits after first 2, are not all same. If the assertion is true, then it matches the next 8 digits.
(\d) captures the first digit in group 1
Then \1 backreferences the captured group to match the same digit that was matched by \d
{7} matches the backreference 7 times. That means - (\d)\1{7} matches same digit 8 times.