Regex and not condition - regex

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

Related

Regex to match a string containing 14 digits and 1 character at any position

I need a regular expression that matches a string of 15 characters where 14 of them are digits and 1 is a character. The character can be in any position of the string.
I have the following long regex:
^.\d{14}|\d{1}.\d{13}|\d{2}.\d{12}|\d{3}.\d{11}|\d{4}.\d{10}|\d{5}.\d{9}|\d{6}.\d{8}|\d{7}.\d{7}|\d{8}.\d{6}|\d{9}.\d{5}|\d{10}.\d{4}|\d{11}.\d{3}|\d{12}.\d{2}|\d{13}.\d{1}|\d{14}.$
Can it be simplified?
Here is a sample match: 1000-1234567890
(?=^.{15}$)\d{0,14}\D\d{0,14}$
First check the string is 15 characters long, then has 0-14 digits, one non-digit, then 0-14 digits.
This isn't exactly the same as the original regex, which allows 15 digits in a row. To get that, simply change \D to .
We can use a lookaround trick here:
^(?!(?:.*\D){2})(?=.{15}$)\d*\D\d*$
This regex pattern says to match:
^ from the start of the string
(?!(?:.*\D){2}) assert that 2 or more non digits do NOT occur (implying at most 1 occurs)
(?=.{15}$) assert length is 15 characaters
\d*\D\d* then a non digit surrounded, possibly, on either side by numbers
$ end of the string

Regex count number of digits excluding certain characters

I have a regex with certain conditions
Only allow digits and some characters ie space . + ( - ) x X
Total digits excluding the characters must be between 8 to 20
I have the first condition, 2nd seems bit tricky for me
^[+\d\s\-\.\(\)xX]{8,20}$
Thank you for your time.
^[ \.\+\(\-\)xX]*(?:\d[ \.\+\(\-\)xX]*){8,20}$
[ \.\+\(\-\)xX]* matches any number of the nonnumeric characters you've listed. We start by doing this at the beginning of the string. (This part can be removed if the string is not allowed to start with one of those characters.)
We then match 8-20 digits, each of which can be followed by any number of said nonnumeric characters.
The 8 to 20 digits requirement is a bit ugly, but we can handle that using positive and negative lookaheads. Consider this version:
^(?=(?:\D*\d){8})(?!(?:\D*\d){21})[\d\s.()xX+-]+$
Explanation:
^ from the start of the input
(?=(?:\D*\d){8}) assert that 8 or digits appear
(?!(?:\D*\d){21}) but assert that more than 20 digits do NOT appears
[\d\s.()xX+-]+ match numbers, whitespace, (), +, x, X or - 1 or more times
$ end of the input

Regex for phone number allowing 9 or 10 digits

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.

RegEx for N digits always starting with 2

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

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