I'm trying to get a regex to validate international phone numbers.
This is what i have so far:
^\+?( |\(|\))*?\d{8,14}$
What I'm aiming for is total allowed digits is 8-14. In addition, to allow as many whitespace and dashes in between, after or before the digits.
Examples:
12345678901234
123 456789 0 1 2 3 4
+1 2345678901234
(12) 3 4 56 7 8 9 01234
1-2-3-4-5-6-7 8 9 0 123 4
I'm not sure how to go about this.
Thanks.
The below regex would help you.
^(?=(?:\D*\d\D*){8,14}$)[ -]*\+?[ -]*(?:\((?:[ -]*\d)+[ -]*\))?[- \d]*
DEMO
OR
^(?=(?:\D*\d\D*){8,14}$)[- \d()+]*
DEMO
\D matches any non-digit character. (?=(?:\D*\d\D*){8,14}$) asserts that the string we are going to match must contain 8 to 14 digit.
Related
I have a regular expression like:
/^([0-9]{2,3})/
This will accepts 2 or 3 number digits between 0 and 9
123 or 12
I need a validate to: if the number has 3 digits, the first should be 0, in our case 023
and if not, number should be the 2 digits one: 12
Can anyone help me?
You may use
^0?\d{2}$
See the regex demo
Details
^ - start of string
0? - an optional 0
\d{2} - two digits
$ - end of string
How I can write a regex which accepts 10 or 14 digits separated by a single space in groups of 1,2 or 3 digits?
examples:
123 45 6 789 1 is valid
1234 567 8 9 1 is not valid (group of 4 digits)
123 45 6 789 109 123 8374 is not valid (not 10 or 14 digits)
EDIT
This is what I have tried so far
[0-9 ]{10,14}+
But it validates also 11,12,13 numbers, and doesn't check for group of numbers
You may use this regex with lookahead assertion:
^(?=(?:\d ?){10}(?:(?:\d ?){4})?$)\d{1,3}(?: \d{1,3})+$
RegEx Demo
Here (?=...) is lookahead assertion that enforces presence of 10 or 14 digits in input.
\d{1,3}(?: \d{1,3})+ matches input with 1 to 3 digits separated by space with no space allowed at start or end.
aggtr,
You can match your use case with the following:
^(?:\d\s?){10}$|^(?:\d\s?){14}$
^ means the beginning of the string and $ means the end of the string.
(?:...) means a non-capturing group. Thus, the part before the | means a string that starts and has a non-capturing group of a decimal followed by an optional space that has exactly 10 items followed by the end of the string. By putting the | you allow for either 10 or 14 of your pattern.
Edit I missed the part of your requirement to have the digits grouped by 1, 2, or 3 digits.
I have to generate a regular expression to detect patterns of text where credit card numbers are involved, I have a regular expression but fails when the text is altered with simple spaces between the text for example (not valid credit card number):
4320 7589 9456 0123
The regex is:
4\d{3}(\s+|-)?\d{4}(\s+|-)?\d{4}(\s+|-)?\d{4}
This regex match easy, but if someone alter the text with spaces between any number like this:
4 320 7589 9456 0123
Does not match, I need a regex to detect any possible variable with spaces, special symbols, letters, some examples:
43 20 75 89 94 56 01 23
4 3 2 0 7 5 8 9 9 4 5 6 0 1 2 3
4320a7589b9456c0123
4320$7589$9456$0123
4320_7589_9456_0123
I don't know if I can strip any space, symbols from the pattern to analyze the text?
I am posting because you actually asked for help with pattern to match any number of non-digits between the first 4 and 15 more digits.
The pattern is
^4(?:\D*\d){15}$
See demo
Regex breakdown:
^ - start of string
4 - literal 4
(?:\D*\d){15} - 15 occurrences of sequences of...
\D* - 0 or more non-digit symbols before..
\d - a digit
$ - end of string
If you need to capture, you can capture (like ^4((?:\D*\d){3})((?:\D*\d){4})((?:\D*\d){4})((?:\D*\d){4})$), but the submatches will still contain the "junk" in-between digits.
This question already has an answer here:
RegEx String, 12 signs (at least 1 number and at least 1 letter)
(1 answer)
Closed 8 years ago.
The whole string contains at least 4 characters (numbers and letters, excluding spaces)
The string contains at least 1 letter
The string contains at least 1 number
Example:
The regex should NOT match:
1
a
12
ab
12a
123
aaa
1 1a
11 a
The Regex Should match:
123a
1234a
123a abcd1
123a 1234
Ab 123
1234 abcd
12 ab
12ab ab12 ab321
123 a
Thank you very much for your help!
You can use this regex:
^(?=.*\d)(?=.*[a-zA-Z])(?=(.*[\da-zA-Z]){4}).{4,}$
RegEx Demo
I need help with the below regex:
(0 ?)([1-8] ?){1}(\d ?){9}|(\d ?){10}
The regex should match either 10 or 11 digits
The first digit should be 0
The second digit should contain from 1 - 8 digits only
All digits can have spaces in between e.g. 0 1 2 7 4 3 3 3 4 4 4
Digits can be without spaces e.g. 01274333444
The regex I created works for most of the scenarios apart from the third condition i.e. The second digit should contain from 1 - 8 digits only.
Any help very much appreciated
Here is what your regex (0 ?)([1-8] ?){1}(\d ?){9}|(\d ?){10} is currently doing:
It's also trying to match 11 or 12 digits instead of 10 or 11.
What you need to do is change it one of the following ways:
Add a group around the OR | to limit it's scope: (0 ?)([1-8] ?){1}(?:(\d ?){8}|(\d ?){9})
Or preferably change it to a 8 or 9 character match: (0 ?)([1-8] ?){1}(\d ?){8,9}
You can use this:
^0 ?[1-8] ?(?:[0-9] ?){8,9}$
I used anchors ^ and $ to ensure that there is no leading or trailing digits.
Try this one:
^(0\s?)([1-8]\s?)(\d\s?){8,9}$