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
Related
I want to match numbers including "-" with non leading zeros and normal numbers without "-". Therefore I want to use a regular expression.
The regex
should match 0 1 2 3 123 2-3 22-33 and
should not match 0123-123 01234.
The following regex works nearly:
\b(0|[1-9][0-9]*\-?[0-9]*)\b
The numbers 0 1 2 3 123 2-3 22-33 and 01234 are matched correctly, but 0123-123 not: it is matched partly. https://regex101.com/r/0Po3Ed/1.
You may use a negative lookbehind in your regex:
(?<!-)\b(?:0|[1-9][0-9]*(?:-[0-9]+)?)\b
Updated RegEx Demo
(?<!-) is negative lookbehind expressions that will fail the match if you have - before numbers.
I am stuck with a problem that challenges me to create the regular expression for binary numbers (containing 0s and 1s). But the string should only match if the binary number only contains a maximum of five 1s. How do I limit a character appearance in a regex?
Examples:
01101101 is correct
01111100 is correct
10110011 is correct
01111110 is wrong
11111110 is wrong
^0*(?:10*){,5}$
Essentially this matches any combination of '1's and '0's but only allows a substring containing a single '1' character to occur five times at most.
Try it out here:
https://regex101.com/r/JKV1Uk/2
Explanation:
^ matches the beginning of the string
0* matches zero or more '0's
(?:10*){,5} matches up to 5 '1's followed by any number of zeros
$ matches the end of the string
If you engine supports lookaheads, and there can not be more than 8 times a 1 or 0 and the 1 should not occur more than 5 times, you might use:
^(?=[01]{8}$)(?!(?:0*1){6,})[01]+$
Explanation
^ Begin of the string
(?= Positive lookahead that asserts that what is on the right side
[01]{8}$ Match 8 times a 0 or 1 until the end of the string
) Close lookahead
(?! Negative lookahead that asserts that what is on the right side
(?:0*1){6,} The pattern zero or more times a 0 followed by a 1 0*1 is not repeated 6 or more times (so 0 till 5 times is valid)
) Close negative lookahead
[01]+$ Match 0 or 1 one or more times
$ The end of the string
How to get
[\d ]{6}
to match:
1 23456
1 2 3456
1 2 3 456
1 2 3 4 56
1 2 3 4 5 6
In other words, I would like the space to not be counted towards the char limit. Something like [\d]{6 + but allow spaces you can eat}
The following will match 6 numbers, with any amount of space characters between them.
(?:\d\s*){5}\d
?: at the beginning there makes the group non-capturing. It's not necessary if all you wish to do is a simple match.
A live example:
https://regex101.com/r/PZJ8DO/2
Just to put my two cents in: you could use the opposite of \d which is \D in most flavors:
^(?:\d\D*){6}$
See a demo on regex101.com.
Note, that this would even allow something like
1a2b3c4d5e6
If this is not what you want (meaning you only want to allow spaces, nothing else), use \s* instead of \D*.
You can try to use
(?<=).*6.*
This will match any line that contains '6' even if there are some white spaces or other characters in the line.
The (?<=) Positive Look Behind.
The . matches any character except line breaks.
The * matches 0 or more of the preceding token.
And 6 matches a "6" Character.
You can test Regular Expression here: RegExr
Note that the positive look behind feature is not supported in all flavors of RegEx.
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
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.