How do I create a regex that matches telephones with or without spaces in the number?
I have found:
^\+?\d+$
From another post but how do I modify that to allow 0 or more spaces in the number?
The first thing you need to think is the exact format you want for phone numbers containing spaces. Eg:
+535 233 4444
Is that one OK? It means divided like: 3 3 4. You can adapt the following regex to your needs:
^\+?\d{3}\s?\d{3}\s?\{d}{4}$
Just change the quantifiers ({3}, {4}, etc) to change the group lengths.
This is one example:
/^(?:\s*\d{3})?\s*\d{3}\s*\d{4}\s*$/
There's a lot of ways to match telephone numbers (and a lot of valid telephone formats). Here's a simple regex to match "5555555555", "555 555 5555", "(555) 555-5555", "555-555-5555", or "555.555.5555"
^(?\d{3})?( |-|.)?\d{3}( |-|.)?\d{4}$
Related
Regex beginner here. I've been trying to tackle this rule for phone numbers to no avail and would appreciate some advice:
Minimum 6 characters
Maximum 20 characters
Must contain numbers
Can contain these symbols ()+-.
Do not match if all the numbers included are the same (ie. 111111)
I managed to build two of the following pieces but I'm unable to put them together.
Here's what I've got:
(^(\d)(?!\1+$)\d)
([0-9()-+.,]{6,20})
Many thanks in advance!
I'd go about it by first getting a list of all possible phone numbers (thanks #CAustin for the suggested improvements):
lst_phone_numbers = re.findall('[0-9+()-]{6,20}',your_text)
And then filtering out the ones that do not comply with statement 5 using whatever programming language you're most comfortable.
Try this RegEx:
(?:([\d()+-])(?!\1+$)){6,20}
Explained:
(?: creates a non-capturing group
(\d|[()+-]) creates a group to match a digit, parenthesis, +, or -
(?!\1+$) this will not return a match if it matches the value found from #2 one or more times until the end of the string
{6,20} requires 6-20 matches from the non-capturing group in #1
Try this :
((?:([0-9()+\-])(?!\2{5})){6,20})
So , this part ?!\2{5} means how many times is allowed for each one from the pattern to be repeated like this 22222 and i put 5 as example and you could change it as you want .
I want to allow only some specific types of phone number format.
Ex:
xxx-xxx-xxxx
+91-xxxxxxxxxx.
I don't know what will be the regular expression for this.
I refered some sites and got this
/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/
which works for 1st one but not worked for +91 format.
Basically, I want to allow India and US numbers only.
2nd Question:
I want a regular expression which will allow +, -, (, ), and the numbers i.e. 0-9 .
Here is a suggestion that accept your inputs, with separators , . and -:
\+91[\s-]\d{10}|\(?\d{3}\)?[\s-]\d{3}[\s-]\d{4}
Try it on regex101
The following inputs are valid:
123-456-7890
123 456 7890
(123) 123-6547
(999)-999-9999
+91-1234567890
+91 1234567890
If you only want to accept - as a separator, change all [\s-] by - in the regex.
are u looking for something like ^(\d{3}-\d{3}-\d{4}|\+91 \d{10})
example
both numbers are placed in group one this way if u work with groups
edit:
are u looking for one like this? this allows the sets of numbers to be seperated with everything exept a letter or a number (\d{3}[^\d\w]\d{3}[^\d\w]\d{4}|\+91[^\d\w]\d{10})
example
I need to validate uk numbers
Below are sample type of number
01457 341235
0229 111111
+1213 3133143
Optional Plus should be allowed at first postion only
Using this regex but not working
^(?:\W*\d){11}\W*$
An actual UK phone number will start with 0 or +44 (the latter being the UK country code), or possibly just 44, followed by nine or ten digits. A regex to capture that would look something like:
^(?:0|\+?44)(?:\d\s?){9,10}$
In this regex, I have allowed the digits to be separated by spaces in any way, because there isn't a single standardized way of breaking down the numbers. You could further narrow this down to certain allowed groupings, if you like, but it would greatly increase the complexity of the regex.
Your question implies you might want something broader or different. As some of your examples aren't valid UK numbers (+1213 3133143, 12345 123456).
You could use something like this to simply match between 10 and 12 digits, with arbitrary spacing, possibly preceded by a +:
^\+?(?:\d\s?){10,12}$
I'm trying to match exactly the following format:
+639201112222
09201112222
and this is all Ive tried so far:
(\+63|0)?\d{10}
the problem is that it match 2920111222 in 29201112222. How can i create a pattern that will match only the formats below?
+63XXXXXXXXXX
0XXXXXXXXXX
+63 or 0 plus 10 digit number only
where X are all digits from 0-9.
Thank you.
You want to do:
((\+63)|0)\d{10}
A regex for various phone number combinations (just quickly used rubular.com for this):
/(^0|[89]\d{2}-\d{3}\-?\d{4}$)|(^0|[89]\d{2}\d{3}\d{4}$)|(^63[89]\d{2}-\d{3}-\d{4}$)|(^63[89]\d{2}\d{3}\d{4}$)|(^[+]63[89]\d{2}\d{3}\d{4}$)|(^[+]63[89]\d{2}-\d{3}-\d{4}$)/
Validates:
0917-411-1111
0917-4111111
917-411-1111
9174111111
+63917-411-1111
63917-894-3454
639172342345
+639174111111
Here's a solution:
http://jsfiddle.net/kZMRx/
I'm looking for a custom RegEx expression (that works!) to will validate common phone number with area code entries (no country code) such as:
111-111-1111
(111) 111-1111
(111)111-1111
111 111 1111
111.111.1111
1111111111
And combinations of these / anything else I may have forgotton.
Also, is it possible to have the RegEx expression itself reformat the entry? So take the 1111111111 and put it in 111-111-1111 format. The regex will most likely be entered in a Joomla / some type of CMS module, so I can't really add code to it aside from the expression itself.
\(?(\d{3})\)?[ .-]?(\d{3})[ .-]?(\d{4})
will match all your examples; after a match, backreference 1 will contain the area code, backreference 2 and 3 will contain the phone number.
I hope you don't need to handle international phone numbers, too.
If the phone number is in a string by itself, you could also use
^\s*\(?(\d{3})\)?[ .-]?(\d{3})[ .-]?(\d{4})\s*$
allowing for leading/trailing whitespace and nothing else.
Why not just remove spaces, parenthesis, dashes, and periods, then check that it is a number of 10 digits?
Depending on the language in question, you might be better off using a replace-like statement to replace non-numeric characters: ()-/. with nothing, and then just check if what is left is a 10-digit number.