PHP RegEx to Extract Phone No

人盡茶涼 提交于 2019-12-25 02:42:06

问题


bid=
id=3D46799862
03005134012
 +92-345-540700(8)
(00996)-345.(123)/4567 
 bidid=3D46799862). +966 235 123456/7
    * Need to make a payment?  You can do it here (https://www.elance.com/p=+92 123 4567891
hp/project/main/projectInvoices.php?bidid=3D46799862).
    /receivingpmbmessages.html).
<a href=3D"http://help.elance.com/entries/20992337-valign=3D"bottom" style=3D"font-s=
ize:10px; color:#817f80;padding:0px;margin:0px;">1-877-435-2623 | Mon-Fri 8=

Testing at http://rubular.com/r/U0YZQ0GlbZ
RegEx (\d|\+)(\+|\d|\(|\)|-| |\.){7,}\b
Expected matches :

  1. 03005134012
  2. +92-345-540700(8)
  3. (00996)-345.(123)/4567
  4. +966 235 123456/7
  5. +92 123 4567891
  6. 1-877-435-2623

incorrect matches :

  1. matching 46799862 but should be ignore beacause complete word is 3D46799862
  2. matching +92-345-540700(8 and it should match +92-345-540700(8)
  3. matching 00996)-345.(123 but it should match (00996)-345.(123)/4567
  4. matching 46799862). +966 235 123456 but it should match +966 235 123456/7
  5. Matching 20992337- but it should be ignored because its /20992337-valign=

回答1:


I assume that you wan't a regular expression that only matches the 6 expected matches.

This regular expression does that.

(?<!(?:\w|\/))(?:\d|\+|\()[\d ()\/\.-]+(?!\w)

It has to start with either a \d, + or (. But only if there is no preceding \w or /.

Then just consume one or more of [\d ()/.-] but not if it ends with a \w

EDIT

(?<...) is a negative lookbehind. In this case it means that the previous charaters cannot match the regex ...

(?!...) is a negative lookahead. So like above it means that the next characters cannot match the regex ...

Both of the above contructs is non-consuming.

(?:...) is a non capturing group




回答2:


(\d|\+|\()(\+|\d|\(|\)|-| |\/){7,}(\d|\))+\b  

This is more accurate...



来源:https://stackoverflow.com/questions/18849158/php-regex-to-extract-phone-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!