Regex limiting a number string - regex

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.

Related

Write regex patterns for matching single digit or double digit where tens place value is 2 or 4

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

Match repeated digits from number

I need regex to check numbers for repeated digits.
All numbers contain 12 digits, first 6 digits we need to skip, so I need to find numbers where every second digit from 7 repeated.
Like this 964632X5X7X3 X - repeated digits
Results
502632959793 - TRUE
125632757773 - TRUE
475632353773 - FALSE
I have try something like this for every digits from 0 to 9:
\d{6}([9]\d[9]\d[9]\d)$
It didnt work.
You may use
^\d{6}(?=(\d))(?:\1\d){3}$
See the regex demo. You may even refactor this regex later if you need to accommodate any x to y amount of repetitions after the first six digits (just replace {3} with the required {x}, {x,} or {x,y} quantifier with the required thresholds).
Regex details
^ - start of string
\d{6} - the first six digits
(?=(\d)) - a positive lookahead that captures the seventh digit into Group 1
(?:\1\d){3} - three occurrences of the digit captured in Group 1 and any single digit
$ - end of string

RegEx for matching digits and one dot with quantifier

I have a specific pattern I'm trying to get. The pattern I'm looking for is the following: 13 digits with a possible dot for a total of min 3 and max 13 digits (including the dot if present) and ending with "/" and number from 1 to 6.
for now I have this pattern
^(\d*|\d*\.?\d*)\/[1-6]$
but this matches 1234/1 or 123456.890123456778/2
but it's not what I need
I tried a few things but I think I missing something
^(\d*|\d*\.?\d*){3-13}\/[1-6]$
Possible match:
1.3/1
123456./2
123456.890123/3
1234567890123/4
123/5
How do I solve this problem?
Your wordings are a little confusing but if I got you correct then you can use this regex,
^(?=.{5,15}$)\d+\.?\d*\/[1-6]$
Explanation:
^ - Start of string
(?=.{5,15}$) - This positive look ahead ensures that the minimum length is 5 and max length is 15 (adding two for last slash and number)
\d+\.?\d* - Starts capturing the text with one or more digits followed by optional dot . and further more zero or more digits
\/[1-6] - Matches a slash and one to six digit
$ - End of string
Regex Demo
Let me know if this works fine for you else list the case for which it doesn't work.

Regex to match a 2-digit number or a 3 digit number

I need to be able to check if a string contains either a 2 digit or a 4 digit number before a . (period).
For example, 39. is good, and so is 3926., but 392. is not.
I originally had (^\\d{2,4).$) but that allows between a 2 and a 4 digit number preceding a period.
I also tried (^\\d{2}.|\\d{4}.$) but that didn't work.
You can use this regex:
^\d{2}(?:\d{2})?\.$
This regex makes 2nd set of \d{2} optional thus allowing to match 12. or 1234. but not 123..
In the expression (^\d{2}.|\d{4}.$), the dots match any character.
Try escaping them to make them match literal dots: (^\d{2}\.|\d{4}\.$)

Regex that matches any 9 digits except last 4 digits can't be 9999 or 0000

I am trying to figure out a regular expression that matches any 9 digits, but the last 4 digits can't be 9999 or 0000. For example, I want the regex to match these:
123456789
123459991
123459990
But not these:
123450000
123459999
I tried negative lookahead. But it doesn't seem to fit in my requirement.
The closest I can get is \d{5}[^\D90]{4}, but with this the last 4 digits can't be 0 or 9 at all, which is not what I want.
It is a special zip code requirement. Any help will be appreciated!
use a negative lookahead after the 5th digit:
^[0-9]{5}(?!0000|9999)[0-9]{4}$
or a lookbehind at the end:
^[0-9]{9}$(?<!0000|9999)
(if your regex flavor doesn't allow a lookbehind with an alternation, use two lookbehinds:
^[0-9]{9}$(?<!0000)(?<!9999)
Your \d{5}[^\D90]{4} regex matches any 5 digits followed by 4 characters other than a non-digit, 9 and 0.
You can use
^(?!\d*(?:9999|0000)$)\d{9}$
Shorter variant: ^(?!\d*(?:9{4}|0{4})$)\d{9}$. See the regex demo
The negative lookahead (anchored at the start) will fail the match if the input contains some digits and ends with either 9999 or 0000.
If we go on optimizing the regex, the most efficient version (basing on what Casimir suggests in his answer) is:
^\d{5}(?!9{4}|0{4})\d{4}$
See the regex demo
Here,
^ - start of the string
\d{5} - exactly 5 digits
(?!9{4}|0{4}) - check (but not match, the index stays after the 5th digit since it is a zero width assertion) if there are exactly 4 9s or 0, and if found, the match is failed (as (?!...) is a negative lookahead)
\d{4} - exactly 4 digits
$ - end of string.