简体   繁体   中英

match phone numbers from text

I have regex in php that matches pretty good phone numbers from some text.

([\+|0](?:[0-9/\-\(\) ] ?){7,25}[0-9])    

good matches: 041 797 - 991 or +386 2 80 55 291 or +386 (0)41 718 827

bad match: 000/000/13 (if it has three zeros in front doesn`t match, maybe?)

How could i remove this bad match?

You can use a negative assertion (?!...) for that.

A similar example was also somewhere in the numberous existing questions on php regex match phone number , just browse around.

Or you could just loop over the match results and filter them by your new criteria.

Why not simply loop through for all of the numbers and then validate that (remove all extraneous characters):

preg_match_all("/\d/",$phone_number,$array);
// $array[0] will be an array of each digit

Then you can validate based on the length (eg decide if it has a country code or not and check it against a stored array or table).

This fixes your regex:

(?!(?:0[ /-]*){2,})([\+|0](?:[0-9/\-\(\) ] ?){7,25}[0-9])

Since you have already worked on your expression to allow certain characters that you see in your dataset, instead of writing a new one from scratch, I worked with yours.

The added phrase at the beginning is a negative lookahead.

(?!(?:0[ /-]*){2,})

Translation: Looking ahead at the beginning of the match, we don't want to see a { zero (optionally followed by characters such as a space, slash or hyphen)} two or more times.

If two zeros are actually okay, change the 2 to a three. Also tweak the characters inside [ /-] depending on what your data looks like.

I checked the regex against your sample data. It works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM