我正在尝试使用该preg_match_all()
函数从 php 中的字符串中提取电话号码。我知道这种模式会'!\d+!'
提取所有数字。但我需要它做更多。我要提取的数字要么以 256 开头,然后是 9 个数字,要么以 078,077,071,079 开头,然后是 8 个数字。谢谢你。
问问题
414 次
3 回答
5
尝试这个:
\b(?:256\d{9}|07[17-9]\d{8})\b
解释:
<!--
\b(?:256\d{9}|07[17-9]\d{8})\b
Options: ^ and $ match at line breaks; free-spacing
Assert position at a word boundary «\b»
Match the regular expression below «(?:256\d{9}|07[17-9]\d{8})»
Match either the regular expression below (attempting the next alternative only if this one fails) «256\d{9}»
Match the characters “256” literally «256»
Match a single digit 0..9 «\d{9}»
Exactly 9 times «{9}»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «07[17-9]\d{8}»
Match the characters “07” literally «07»
Match a single character present in the list below «[17-9]»
The character “1” «1»
A character in the range between “7” and “9” «7-9»
Match a single digit 0..9 «\d{8}»
Exactly 8 times «{8}»
Assert position at a word boundary «\b»
-->
于 2012-05-03T13:44:54.923 回答
1
于 2012-05-03T13:47:29.360 回答
1
我并不声称自己是正则表达式的专家,但我测试了一个有效的。
(^(256[0-9]{9})|^(078|077|071|079)[0-9]{8})
我针对以下情况进行了尝试:
256123456789
07812345678
07712345678
07112345678
07912345678
07212345678
注意最后一个不符合你说的规则之一。输出与最后一个匹配。
于 2012-05-03T13:39:52.717 回答