How to select phone number from a paragraph using regular exp - php

I want to select phone number from a paragraph.But the format of phone numbers are
123-456-7890
123.456.7890
123*456*7890
(202) 553-6381
123_456_7890
1234567890
1.2.3.4.5.6.7.8.9.0
1*2*3*4*5*6*7*8*9*0
954 665 53 92
456 7890 x123
I got a regular expression but it not work in all cases.The regular expression is
$pat1="/(\d)?(0-9)|(\s|-|_|.|)?(\()?(\d){3}(\))?(\s|-|_|.){1}(\d){3}(\s|-|_|.){1}(\d){4}/";

It may not be the best but it will match all instances
(\d{3}([\-\.\*\s_]?\d{3})([\-\.\*\s_]?\d{4}))|\d(([\.\*]\d){9})|(\(\d{3}\)\s\d{3}\-\d{4})|(\d{3}\s\d{3}\s\d{2}\s\d{2})|(\d{3}\s\d{4}\sx\d{3})

a simple uncompressed regex can be this:
^\d{3}\s\d{3}\s\d{2}\s\d{2}|\d{3}\s\d{4}\sx\d{3}|\d{10}|\(?\d{3}\)?[\s-.*_]\d{3}[-.*_]\d{4}|(\d[*.]){9}\d$
note that in last line there is x123, if it is a four digit number you must change regex to:
^\d{3}\s\d{3}\s\d{2}\s\d{2}|\d{3}\s\d{4}\s\d{4}|\d{10}|\(?\d{3}\)?[\s-.*_]\d{3}[-.*_]\d{4}|(\d[*.]){9}\d$
also, ^ means begins with, and $ means ends with. for some regex queries you must remove them.

Related

Regex to get street number with spaces

I'm still a regex newbie.
I have "nicely formatted" addresses and the data source will only give me nice Australian addresses.
I've got this far:
~([\w\d\-\/\.]*)\s*([\w\d '\-\.\ ()]+)~
Given the address,
123/500-550 Main Street
It will give me two groups (which is what I want):
123/500-550
Main Street
But I'm stuck on trying to accommodate spaces like:
123 / 500-550 Main Street
123 / 500-550 Main Street
123 / 500 - 550 Main Street
Can I maybe use a ^ and lookahead to detect the start of the street name like [\w\d '\-\.\ ()]+ and then get everything to left of it? If so, how?
https://regex101.com/r/kG32Sz/1
You can add whitespace to the number part (removing letters btw) and detect the street part start using positive lookahead:
([\d\-\/\.\s]*)(?=\s+\w)\s+([\w\d '\-\.\ ()]+)
Demo
Though generally not advisable, you could use
^ # start of line
(?P<street_number>[-/\d\h]+)\h+ # capture -, \d and \h => street_number
(?P<street_name>[A-Z][\w\h]+) # capture sth. with UPPERCASE,
# followed by \w and \h => street_name
$ # end of line
See a demo on regex101.com (and mind the modifiers !).
Better use a library with previously known street names instead (or query it with the expression above, that is).
You may add other allowed characters in the class, such as [-'.\w\h]. Note that most characters do not need to be escaped within a class.

Regular Expression in PHP to extract all possible combination of phone/mobile numbers using one Regex

i am using php and wants to extract phone/mobile numbers from string, i have string with multiple format of phone numbers like
$str = '(123) 456-7890 or (123)456-7890 and 1234567890 test "123.456.7890" another test "123 456 7890"';
i had write one RE as,
$phoneMatches = '';
$str = '(123) 456-7890 or (123)456-7890 or 1234567890 or "123.456.7890" or "123 456 7890"';
$phonePattern = '/\b[0-9]{3}\s*[-]?\s*[0-9]{3}\s*[-]?\s*[0-9]{4}\b/';
preg_match_all($phonePattern, $str, $phoneMatches);
echo "<pre>";
print_r($phoneMatches);
exit;
but it gives me output like this,
Array
(
[0] => Array
(
[0] => 1234567890
[1] => 123 456 7890
)
)
Means only two, but i want all the possible combination of phone numbers and mobile numbers from string of text by using only ONE Regular expression.
Thanks
I know I'm late, and I'm not sure if this is what you wanted, but I came up with this solution:
[+()\d].*?\d{4}(?!\d)
Demonstration: regex101.com
Explanation:
[+()\d] - We start by matching anything that might represent the start of a phone number.
.*?\d{4} - Then we match anything (using a lazy quantifier) until we reach four ending digits. Just a little note: I considered this as a rule, but it might not always apply. You'd then need to modify the regex to include other cases.
(?!\d) - This is a negative lookahead and it means that we don't want any matches followed by a digit character. I used this to avoid some half-matches.
Another observation is that this regex doesn't validate any phone number. You could have anything in between the matches, mainly because of this part: .*?\d{4}. This will work depending on what kind of situation you intend to use it.

Check if a String with a only Number and space in PHP

I try check a string is number or not.
number format:
2222 or 22 22 or 1 2222 or 22323423
(with space before,midlle and after digits)
Is better I use regular expression?
So how can I change it to do that?
preg_grep('~^[0-9]$~'
or Is there any faster method?
You can use this regex:
^\d+( +\d+)*$
This will allow 1 or more spaces only in the middle of the number but not at start or end.

My regex for testing phone issue

I want to validate these phone number formats:
517123123
+48517123123
+48 517 123 123
(48)517123123
(48)517 123 123
517-123-123
48 517-123-123
48/517-123-123
48 517 123 123
I wrote this regex:
(\+?)+(((\(([0-9]+){2,2}\)))|(([0-9]+){2,2})?)+(\/?)+(\s?)+(([0-9]+){9,9}|([0-9]+){3,3}(\s|-){1,1}([0-9]+){3,3}(\s|-){1,1}([0-9]+){3,3})
The problem is that it's makes big numbers like 8978978979878978967 valid. Where is my mistake?
Looking at just the end of the regex, I see something that you seem to be doing in multiple places;
([0-9]+){3,3}
The + says at least one repeat of [0-9], which makes 1111111111111 a perfectly valid match. You then limit it to exactly 3 of those matches, which can still be a very long number.
If you want exactly 3 digits, remove the +.
may be you lost anchors.... however, use my regex ^(\+?(\(\d{2}\)|(\d{2})|(\d{2}[/ ])))?((\d{3} \d{3} \d{3})|(\d{3}-\d{3}-\d{3})|(\d{9}))$
At the moment I can't see what your regex is doing, there is too much superfluous stuff in it.
You have too many groups
You want to repeat optional characters!?
e.g.:
(\+?)+, you don't need a group around and you don't want to repeat that, so \+? is what you want here.
(\s?)+, do you want to say "0 or more whitespaces"? Then \s* is what you need.
When you write e.g. {9,9}, then you can remove one digit, {9} is the same.
You are nesting quantifiers, thats the place where you allow too many characters. You have multiple places, where you do ([0-9]+){9,9}, that means 1 or more digits and repeat that 9 times.

PHP regular expressions (phonenumber)

I'm having some trouble with a regular expression for phone numbers. I am trying to create a regex that is as broad as possible for european phone numbers. The phone number can start with a + or with two leading 0's, followed by a number in between 0 and 40. this is not necessary however, so this first part can also ignored. After that, it should all be numbers, grouped into pairs of at least two, with a whitespace or a - inbetween the groups.
The regex I have put together can be found below.
/((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}/
This should match the following structures
0031 34-56-78
0032123456789
0033 123 456 789
0034-123-456-789
+35 34-56-78
+36123456789
+37 123 456 789
+38-123-456-789
...
What it also matches according to my javascript
+32 a54b 67-0:
So I must have made a mistake somewhere, but I really can't see it. Any help would be appreciated.
The problem is that you don't use anchors ^ $ to define the start and ending of the string and will therefore find a match anywhere in the string.
/^((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}$/
Adding anchors will do the trick. More about these meta characters can be found here.
Try this, may be can help you.
if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$",$var))
{
$valid = true;
}
Put ^ in the beginning of the RegExp and $ in the end.

Categories