正则表达式模式从文本中提取特定数字

rea*_*leo -6 php regex preg-match-all

我试图使用该preg_match_all()函数从php中的字符串中提取电话号码.我知道这种模式会'!\d+!'提取所有数字.但我需要它做更多.我要提取的数字要么以256开头,后跟9个数字,要么是078,077,071,079,后跟8个数字.谢谢.

Cyl*_*ian 5

试试这个:

\b(?:256\d{9}|07[17-9]\d{8})\b
Run Code Online (Sandbox Code Playgroud)

说明:

<!--
\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»
-->
Run Code Online (Sandbox Code Playgroud)