in reply to Regex for extracting phone numbers from string
I'm trying to write a regex to extract phone numbers from a string. [...] As you can see, my regex does not handle examples #2, #5 and #9 properly. When there are long strings of digits, it finds phone number where there are none. How can I modify the regex to avoid these false positives? I've tried a few different ways but none of them succeeded.
Are you aware that people write phone numbers in very different ways around the world? Some examples:
In Germany, "(0 12 34) 5 67 89 - 11" is a very common way to write a business phone number, "0 12 34 / 5 67 89 - 11" is as common. "(0 98 76) 54 32 10 98" may be a private phone number (no extension after "-"), it could also be written as "0 98 76 / 54 32 10 98". In business context, "+49 (0) 12 34 / 5 67 89 - 11" is also used. International contacts would dial the international call prefix, 49 for germany, 12345678911 to reach the contact, german callers would ignore the "+49" and dial 012345678911. Spacing varies wildly, ignoring the rules that digits should be written in pairs. People trying to stand out from the masses add dots, underscores or some other line noise to their phone number. Others just omit all whitespace: +49(0)1234/56789-11.
Speaking of international call prefixes (the "+" in "+49"): Germany and many other countries use 00, the USA use 011, Australia uses 0011, Cuba uses 119, some former soviet republics use 810, and so on. In GSM networks, you can directly dial "+".
Vanity numbers seem to be quite popular in some areas of the world. "555-SHOE-SHOP" may be a valid phone number in the US, "(0700) PERLMONK" may be a valid phone number in Germany. Because the masses still do not understand vanity numbers, the latter is usually also written out in digits: "(0700) 73756665".
000, 110, 112, 117, 118, 144, 911, 999 are unusually short, but still valid phone numbers, all for emergency use in various areas of the world. Not all emergency numbers work everywhere. Germany only uses 110 and 112. 117 and 118 are used in the Swiss, 911 in the USA, 999 and 112 in the UK, 000 in Australia, 144 in Swiss and Austria. France also uses the two-digit numbers 15, 17, and 18 as emergency call numbers.
Don't assume three digits = emergency use. While most phone numbers in Germany have at least four, usually six to eight digits, my brother has an ancient three-digit phone number, recently migrated to VoIP. As long as those ancient numbers are in use, they can not be revoked or changed. So some people still have and use those exceptionally short numbers in Germany.
See also Telephone_number, National_conventions_for_writing_telephone_numbers, Re: Trim Phone Numbers.
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex for extracting phone numbers from string
by dsheroh (Monsignor) on Apr 11, 2016 at 07:32 UTC | |
|
Re^2: Regex for extracting phone numbers from string
by nysus (Parson) on Apr 11, 2016 at 13:08 UTC |