Need help finding a phone number and a string using regular expressions - regex

I am new to regex and I an trying use regex to find any number of characters that comes after the question mark and before the pipe, along with the phone number. Can anyone please help me? Below is a sample URL ?
http://www.myurl.com?demo|15555555555
regular expression should find "demo" and "15555555555"

here is a regex with named capturing groups, also I tried to cover many possibilities for different number formats
Regex
(?<=\?)(?'name'[\w\s%]+)\|(?'phone'[\+\w-\(\)\s%]+)(?:\n|$)
Test string
http://www.myurl.com?demo|15555555555
http://www.myurl.com?Local|754-3010
http://www.myurl.com?Domestic|(541) 754-3010
http://www.myurl.com?International|+1-541-754-3010
http://www.myurl.com?Dialed in the US|1-541-754-3010
http://www.myurl.com?Dialed from Germany|001-541-754-3010
http://www.myurl.com?Dialed from France|191 541 754 3010
http://www.myurl.com?skype|155555-SKYPE
http://www.myurl.com?an%20escaped%20name|191%20541%20754%203010
Result
MATCH 1
name [21-25] demo
phone [26-37] 15555555555
MATCH 2
name [59-64] Local
phone [65-73] 754-3010
MATCH 3
name [95-103] Domestic
phone [104-118] (541) 754-3010
MATCH 4
name [140-153] International
phone [154-169] +1-541-754-3010
MATCH 5
name [191-207] Dialed in the US
phone [208-222] 1-541-754-3010
MATCH 6
name [244-263] Dialed from Germany
phone [264-280] 001-541-754-3010
MATCH 7
name [302-320] Dialed from France
phone [321-337] 191 541 754 3010
MATCH 8
name [359-364] skype
phone [365-377] 155555-SKYPE
MATCH 9
name [444-463] an%20escaped%20name
phone [464-486] 191%20541%20754%203010
try demo here

if you create that link yourself, I would recommed you to write a common $GET-variable. you won't need any regex.
http://www.myurl.com?demo=15555555555
echo htmlspecialchars($_GET["demo"]);

You can use this regex:
\?(\w+)\|(\d+)
Working demo
MATCH 1
1. [21-25] `demo`
2. [26-37] `15555555555`

Related

UK Mobile phone and UK landline regex

I have the following 2 regex for UK mobile phone number and for UK landline numbers respectively-
^0(7\\d{9})\$
^0([1-6]\\d{8,10})\
I need to combine them into one regex to validate an input field using JavaScript to be any UK mobile or landline. I’ve tried using the | (pipe symbol) for ‘or’, but that’s not working and doesn’t recognise either of them.
Anyone any suggestions as to how best to combine.
Many thanks in advance,
If you want to combine the patterns, you may use
^0([1-6][0-9]{8,10}|7[0-9]{9})$
Note that the common prefix is outside the parentheses, the rest is an alternation group with anchors outside of the group.
Details
^ - start of string
0 - a zero
([1-6][0-9]{8,10}|7[0-9]{9}) - either of the two alternatives
[1-6][0-9]{8,10} - a digit from 1 to 6 and then 8 to 10 digits
| - or
7[0-9]{9} - 7 and any 9 digits
$ - end of string.

Regex for telephone number with or without spaces

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}$

regex for phone number validation?

i want to validate my phone number with the regex for following formats.i have googled the things but i could not find the regex for following formats...
079-26408300 / 8200
(079) 26408300
079 264 083 00
9429527462
can anyone please guide me how can i do validate the phone number field for above formats?
I want to validate the phone number for only above formats as right now am using only following regex var phone_pattern = /^[a-z0-9]+$/i;
#Ali Shah Ahmed
var phone_pattern = "(\d{10})|(\d{3}-\d{8}\s/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})";
here is the way am checking if its valid
if (!phone_pattern.test(personal_phone))
{
$("#restErrorpersonalphone").html('Please enter valid phone number');
$("#personal_phone").addClass('borderColor');
flag = false;
} else {
$("#restErrorpersonalphone").html('');
$("#personal_phone").removeClass('borderColor');
}
its not working. Am I implementing in wrong way?
lets start with the simplest phone number 9429527462
As this has 10 characters and all are numbers, regex for it could be \d{10}
Now the next phone number 079 264 083 00. Regex for this pattern could be (\d{3}\s){3}\d{2}
First we are expecting a group of 3 digits and a space to repeat thrice (\d{3}\s){3}, this will cover 079 264 083 (space included in it), so left will be the last two characters which are handled using \d{2}
For the phone number (079) 26408300, \(\d{3}\)\s\d{8} regex could be use. The regex first looks for a opening bracket, then three digits inside it, and then the closing bracket. It then looks for a space, and then for 8 digits.
The phone number 079-26408300 / 8200 could be validated using regex \d{3}-\d{8}\s/\s\d{4}. It first looks for 3 digits then a -, then 8 digits followed by a space. Then looks for a / and then a space and then 4 digits.
If you wish to know a single regex for validating all the above patterns, do let me know.
Final combined regex would be:
/(\d{10})|(\d{3}-\d{8}\s\/\s\d{4})|((\d{3}\s){3}\d{2})|(\(\d{3}\)\s\d{8})/
Straightforward solution is simple, use |
String ex = "\\d{3}-\\d{8} / \\d{4}|\\(\\d{3}\\) \\d{8}|...

ReGex Phone No Format in Philippines

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/

Custom RegEx expression for validating different possibilities of phone number entries?

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.