RegEx for N digits always starting with 2 - regex

I want to validate these rules:
1)Only numbers
2)Must have 13 digits
3)Always start with number 2
4)May have dots after the first 8 digits, 2 digits and before last
digit like:
(XXXXXXXX.XX.XX.X)
Example:
2437313600001 - 23610579.00.03.1
So far I have this
^([0-9]-?){13}$
How do I solve this problem?

You can use this regex,
^2\d{7}(?:\.?\d){5}$
Explanation:
^ - Start of string
2 - Start first character with 2 only
\d{7} - Next seven characters can be any digits
(?:\.?\d){5} - Next five characters can be any digits but they can be preceded by an optional dot before them
$ - End of string
Regex Demo

Related

How to ensure that a string contains numbers and letters by using RegEx

I'm trying to match strings that either:
Contains between 9 and 15 numbers (Only numbers)
Contains between 6 and 15 numbers+letters (it must contain both, numbers and letters. Only letters is not a valid option).
I have the following regex: \b([0-9]{9,15})|([A-Za-z0-9]{6,15})\b which fails because the second part allows you to have a string with 6 numbers or 6 letters only.
Should be valid:
123456789
12345678Y
Y234Y2
Should not be valid:
12345678
123X4
ABCDEFGHYJ
You can use
^(?:[0-9]{9,15}|(?=[a-zA-Z]*[0-9])(?=[0-9]*[a-zA-Z])[A-Za-z0-9]{6,15})$
See the regex demo.
Details:
^ - start of string
(?: - start of a non-capturing group:
[0-9]{9,15} - nine to 15 digits
| - or
(?=[a-zA-Z]*[0-9])(?=[0-9]*[a-zA-Z])[A-Za-z0-9]{6,15} - six to 15 alphanumeric chars with at least one digit and at least one letter
) - end of the group
$ - end of string.

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 and not condition

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

Regex limiting a number string

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.

How to match a whole string that contains just two and no more than two digits between 0 and 10 in regex?

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