関数を使用して、phpの文字列から電話番号を抽出しようとしていpreg_match_all()
ます。'!\d+!'
このパターンがすべての数値を抽出することを私は知っています。しかし、私はそれをもっとする必要があります。抽出したい数字は、256の後に9つの数字が続くか、078,077,071,079の後に8つの数字が続くかのいずれかです。ありがとうございました。
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 に答える