Regex to validate telephone number, must exclude leading zeros more than 1 - regex

I have the following regex with which i am able to validate for telephone numbers against different types of numbers and it is working correctly (Landline + mobile).
(^[0][1-9]\d{8,}|^[+]\d{11,})(;ext=\d{1,})?$
To this I am trying to add one more condition to it, with leading zero taken into consideration.
If the number does not begin with a zero, then the above regex is applied.
If the number begins with a zero, and has more than 1 zeros in the beginning, then it should use the regex, but with only 1 zero in the beginning while discarding other leading zeros.
Other zero's contained with the number are not an issue
Example:
Number - 0012345678901
With my current regex this fails as pattern is not matched. If i remove one zero in the start, pattern is valid (012345678901).
I have modified my regex to the following. Can I write this better?
(^[0][1-9]\d{8,}|^[0][0][1-9]\d{8,}|^[+]\d{11,})(;ext=\d{1,})?$

^(0{0,2}[1-9]\d{8,}|^[+]\d{11,})(;ext=\d{1,})?$
In this regex, the first part (0{0,2}[1-9]\d{8,}) matches phone numbers that start with zero, zero or one zeros followed by a digit between 1 and 9, and then any 8 or more digits. This pattern will match phone numbers that have one or two leading zeros and then discard any additional zeros. The second part |^[+]\d{11,} matches phone numbers that start with a plus sign and have 11 or more digits.
Note that this updated regex assumes that phone numbers with leading zeros are only valid if they have at least one digit between 1 and 9 after the leading zeros. If you also want to allow phone numbers with all zeros after the leading zeros, you can modify the regex to (0{0,2}\d{9,}|^[+]\d{11,}).

Related

Validating a phone number with a custom rule possibly using nested groups

I am trying to write a regex for validating phone numbers.
We have custom rules, i.e. the phone number must meet the following pattern:
+ or 00 as a prefix
One to three digits
An optional space or hyphen
Then one to n digits (n is still constrained by the rule for total character count below
The total number of characters must not exceed 28.
Here is the regex I have come up with:
/^((\+|00)(\d{1,3})[\s-]?)(\d{1,23}){1,28}$/
I am sure it can be simplified. Can someone please help?
This part of your pattern (\d{1,23}){1,28} matches 1-23 digits followed by repeating that 1-28 times and the maximum is 28×23=644 (Thank you #Toto)
You could check if the string consists of 1-28 times the listed characters using a positive lookahead (?=[+\d -]
The last part currently is \d{1,}, but you could specify a minimum length if you don't want to match +1 1
Note that \s could also possibly match a newline.
^(?=[+\d -]{1,28}$)(?:\+|00)\d{1,3}[ -]?\d{1,}$
Regex demo

Regex fixed amount of number with fixed amount of spaces [duplicate]

i am trying to write a regular expression to validate the numbers with only one space in undefined places?
Maximum of 12 characters with one space or Maximum of 11 characters without spaces.
Ex: '25897 569874','5674','65783987665','435 6523'
i have tried with ^[0-9]{0,12}$.this is not perfect cause I don't know how to place the spaces and its counts.
You can use this regex:
^(?:\d{1,11}|(?=\d+ \d+$)[\d ]{3,12})$
\d{1,11} will match from 1 to 11 digits without space.
(?=\d+ \d+$)[\d ]{3,12} will match up to 11 digits with one space somewhere in the middle. The space cannot be leading or trailing, so ' 23' will be rejected.
(?=\d+ \d+$) is a look-ahead that matched one or more digit, then a space, then one or more digit, then anchor the end of string. It guarantees only one space will appear and the space will not be leading or trailing. The look-ahead also implicitly confirms that there are at least 3 characters in the string.
[\d ]{3,12} will guarantee the string only contains digits or space, and up to 12 of them. The lower bound of number of repetition can be set to 3 or lower, since it has been implied by the look-ahead.
The 2 constraints together guarantees that text contains from 1 to 11 digits and an optional space at arbitrary position in between the digits.
To allow leading space, but reject single space, empty string and trailing spaces:
^(?:\d{1,11}|(?=\d* \d+$)[\d ]{2,12})$
Again, the look-ahead implies at least 2 characters, so the number of repetitions can be set to 2 or lower.
^[0-9 ]{0,12}$ will match upto 12 character string with or without space
If you need multiple criteria,
try OR operator (pipe): |
^[0-9 ]{0,12}$|another condition

RegEx Expression /w limited length and chars

My question is NOT similiar to Regular expression to match numbers between 1-5000, because I have something else to check (no range, leading and no leading zeroes, etc.).
I have the following RegEx in an input check:
^[0-9]{1,4}$
This works for digits with 1-4 length.
Unfortunately I have to allow only these combinations:
One digit numbers 1-9 (no leading zeroes)
or two digits numbers 10-30 (no leading zeroes)
or four digits numbers 0001-9999 (with leading zeroes).
What RegEx do I need?
Let's define subpatterns for your conditions:
One digit numbers 1-9 (no leading zeroes) - Use [1-9]
or two digits numbers 10-30 (no leading zeroes) - Use [12][0-9]|30
or four digits numbers 0001-9999 (with leading zeroes) - Use [0-9]{4}
So, the whole pattern should be built from those alternatives that should be anchored and grouped (so that the anchors were applied to the group)
^([1-9]|[12][0-9]|30|[0-9]{4})$
See the regex demo

regular expression to match all zeros, length from 1-6

I am trying to check if a number is composed of all zeros, the number can be from 1-6 digits, it just can't be all zeros. I came up with this, but this only works if the length is 6. how can I make it so it checks lengths from 1-6?
(?<!\d)(?!000000)\d{6}(?!\d)
the number can be from 1-6 digits, it just can't be all zeros.
You can use this lookahead based regex:
^(?!0+$)\d{1,6}$
(?!0+$) is negative lookahead to fail the match if input has all zeroes.
^\d{1,6}# will match 1 to 6 digits in input

Regex for allowing numbers without leading and ending 0

I want except some numbers in different syntax and I am trying to find the best Regex for this task/match.
First some valid numbers:
0.01
0.2
0.38
45
165.6
52732.08
999999999.99
And here some invalid numbers:
.01
.2
.50
.85
45.
45.0
45.00
00045.0
124.60
000124.60
124,6
000053853.01
999.999.999,99
999999999,99
After several tests I have created the following Regex:
^[1-9]?\d{1,9}\.?\d{1,2}(?<!0)$
But I always struggling on the number: 000058723.01
Any ideas? Thanks.
You can use this regex:
^(?!0+\d)\d+(?:\.(?![1-9]*0+$)\d{1,2})?$
Or:
^(?:0+|[1-9]\d*)(?:\.(?![1-9]*0+$)\d{1,2})?$
RegEx Demo
Try this pattern:
^((?:0|[1-9]+)(?:\.(?:\d+?[1-9]|[1-9]))?)$
Demo
You accept four kinds of input:
A number with no decimal places and without leading zeroes: [1-9]\d*
Zero followed by a dot followed by digits (without trailing zeroes): 0\.\d*[1-9]
A decimal number without leading or trailing zeroes: [1-9]\d*\.\d*[1-9]
Zero: 0
Putting the four together:
^([1-9]\d*|0\.\d*[1-9]|[1-9]\d*\.\d*[1-9]|0)$
Here is a fixed version of your regex:
^(?!0{2,})\d+(?:\.\d{1,2}(?<!0))?$
Here, initial 2 or more zeros are not allowed with the lookahead (?!0{2,}), and the decimal part is made optional within a non-capturing group (?:\.\d{1,2}(?<!0))?.
See demo
In case you do not want to match 0, you can exclude this in the negative lookahead:
^(?!0{2,}|0$)\d+(?:\.\d{1,2}(?<!0))?$
^^
See Demo 2
A number with optional decimals is composed from two pieces: the integer part and the optional decimal part that starts with a dot.
The integer part is either zero (0) or a sequence of digits that start with 1..9 (no 0) and can continue with zero or more digits:
0|[1-9][0-9]*
If you need to impose an upper limit on the integer part's length then replace * with {,n} where n is the maximum allowed length minus 1.
The decimal part starts with a dot (.) followed by zero or more digits and followed by one of 1..9 (no 0 allowed at the end).
The expression is:
\.[0-9]*[1-9]
Now let's combine them:
^(0|[1-9][0-9]*)(\.[0-9]*[1-9])?$
What I added when I joined the pieces:
^ - match the start of the string; without this the regex matches 45.0 from 00045.0;
parentheses around the integer part because of the lower precedence of |;
parentheses around the decimal part, followed by ? to signal the entire decimal part is optional;
$ - match the end of the string to avoid matching 124.6 from 124.60.
Remarks
The above regex was designed to match your examples. However, please notice that most programming languages allow most or all of the numbers you put in the "invalid" section and use a dot (.) as decimal separator. And many languages provide library functions that are able to parse the numbers that use a comma (,) as decimal separator.
Numbers without integer part (.85), without digits after the dot (45.) ore with trailing zeros (45.0) are valid and are interpreted without ambiguity.
The only troublemaker is the leading zero (00045.0). For integer numbers, most of the times it is a signal that the number is represented in base 8 while for real numbers it is simply ignored.