I am trying to validate a regex which
allows 10 digits
if digit starts with 672 then it should only allow total 9 digits
I have tried below regex
/^\d{10}$|^(672)\d{6}$/
https://regex101.com/r/0ahnKx/1
It works for 10 digits but if number starts with 672 then also it allows 10 digits.
Could anyone help how can I fix this?
Thanks!
First of all, the capturing group in your regex is redundant, it would make sense to wrap the part of the pattern between ^ and $ to only use single occurrences of the anchors.
To fix the issue, you need to make sure the first three digits matched by \d{10} are not 672, and you can achieve that with a negative lookahead:
/^((?!672)\d{10}|672\d{6})$/
/^(?:(?!672)\d{10}|672\d{6})$/
See the regex demo. Details:
^ - start of string
(?: - start of a group:
(?!672)\d{10} - no 672 substring check is triggered and then ten digits are matched
| - or
672\d{6} - 672 and six digits
) - end of the group
$ - end of string.
Related
How can I get an regex expression that allows:
0.00 to 50.00 and also comma as decimal separator so
0,00 to 50,00
I have gotten as far as ([0-5]{1})?([0-9]{1})([,][0-9]{1,2})?
but there are still situations in which it fails. I have searched on line but could not find the right answer.
ADDED:
A small change in requirements. Actually it should be from 0.01 to 50.00, and 0,01 to 50,00. (But with the answers below I think I managed to adapt the regex strings so this is also matched)
There is a regex for range generator here. Generated and played with it, came up with this
^(?:[1-4]?\d[.,]\d\d?|50[.,]00?)$
Added non capturing group, little modifications and ^ start / $ end anchor. See demo at regex101.
For optional decimal part, how about this: ^(?:[1-4]?\d(?:[.,]\d\d?)?|50(?:[.,]00?)?)$
This regex should cover almost all cases matching numbers from 0.00 to 50.00,
^(?=.)(?:(?:(?:0|[1-4]?\d)?(?:[,.]\d{1,2})?)|50(?:[.,]00?)?)$
Explanation:
^ - Start of string
(?=.) - Positive look ahead to avoid matching empty string
(?: - Start of first non-grouping pattern
(?: - Start of second non-grouping pattern
(?:0|[1-4]?\d)? - This matches whole number from 0 to 49 and this whole number could be absent
(?:[,.]\d{1,2})? - Matches a comma or dot followed by one or two digits and this decimal part can be absent
) - Closing of second non-grouping pattern
| - Alternation for number 50 case
50(?:[.,]00?)?) - Matches 50 followed by either comma or dot followed by 0 or 00 where decimal part is optional
) - Closing of first non-grouping pattern
$ - End of string
Regex Demo
Edit:
For discarding zero value numbers, you can just add a negative lookahead (?!0*[.,]?0*$) in current regex and use this regex,
^(?=.)(?!0*[.,]?0*$)(?:(?:(?:0|[1-4]?\d)?(?:[,.]\d{1,2})?)|50(?:[.,]00?)?)$
Regex Demo rejecting zero valued numbers
does this one work for you?
(50([.,]0{1,2})?)|([0-4]?[0-9]([.,][0-9]{1,2})?)
I have below two regex expressions
\d{13,16} - Number containing digits 0-9 of length 13 to 16
599999\d{10} - Number starting with 599999 followed by digits 0-9 and of length 16 digits
I want to say A and NOT B. How can I do that.
Thanks in advance for any help.
Try this:
^(?!599999\d{10})\d{13,16}$
Click for Demo
Explanation:
^ - asserts the start of the string
(?!599999\d{10})- negative lookahead to validate that the string does not start with 599999 followed by 10 digits
\d{13,16} - matches 13 to 16 digits
$ - asserts the end of the string
Edit:
If these numbers are in the same line, you can use this instead \b(?!599999\d{10})\d{13,16}\b
So i got this regex code /[0-3][0-9][0-1][1-9]\d{2}[-\s]\d{4}?[^0-9]*|[0-3][0-9][0-1][1-9]\d{2}\d{4}/
This regex code take this kind of numbers:
1002821187
100282 1187
100282-1187
But i found out i dont want the numbers: 1002821187
So is it possible to make 1 regex code that only finds:
100282 1187
100282-1187
Your regex contains an alternation that matches the numbers with and without the space or -. You need to require that space or hyphen:
^[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}$
^^^^^
See the regex demo. If you do not need to check for any boundaries, remove ^ and $ anchors that make the pattern match the whole string and use [0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}. Or use word boundaries to find whole words, \b[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}\b.
Details
^ - start of string
[0-3] - a digit from 0 to 3
[0-9] - any digit
[0-1] (=[01]) - 0 or 1
[1-9] - any digit other than 0
[0-9]{2} - 2 digits
[-\s] - a - or whitespace
[0-9]{4} - 4 digits
$ - end of string.
Though you did not specify exactly what you are trying to do, here you can try the following regex :
\b\d{6}-\d{4}\b|\b\d{4}\b|\b\d{6}\b
Hope it helps.
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 need to check by Regex expression if 9 or 14 digits are typed.
The expression \d{9}|\d{14} seems to be not working properly, what's wrong ?
This regex should work.
^(\d{9}|\d{14})$
Could you post the piece of code you're using and tell us what language are you using?
If you're using regex chances are that you have a string, and I'm sure your language has something to count string length.
EDIT:
as Rubens Farias pointed out in comments maybe ^...$ is needed because your regex would match any number with more than 9 digit as that number has a substring with a 9 digit long number.
Anyway check if you can do it with your language's string's methods/functions
You can use:
^(?:\d{9}|\d{14})$
Explanation:
^ - Start anchor
(?: - Start of non-capturing group
\d{9} - 9 digits
| - alternation
\d{14} - 14 digits
) - close of the group.
$ - End anchor
alternatively you can do:
^\d{9}(?:\d{5})?$
which matches 9 digits followed by optional 5 digits.
(\d{9}|\d{14}) should work