2012-02-20 8 views
1

一部のデータでは電話番号と一致する正規表現を書くのに助けが必要です。 Somin次の形式:特定の形式の電話番号と照合するためのREGEXの検索

XXX-XXX-XXXX 
XXX-XXX-XXXX Ext. XXX 
XXX-XXX-XXXX Ext. XXXX 
(XXX) XXX-XXXX 
XXX-XXX-XXXX (Text) 
XXX.XXX.XXXX 

、具体的に、この行に1つの正規表現:

XXX-XXX-XXXX (Text & Special Chars.); XXX-XXX-XXXX (Text) 

私はいくつかの質問を通じて検索し、これまでの一般的な0-9の一致を試みたが、無駄にしました。私はまた、通常の電話番号を取得するために、より最近では、これを試してみました:

preg_match_all('/^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/', $a3, $n); 

print_r($n); 


誰かが私に右の道を示すことはできますか?ありがとう

+2

http://stackoverflow.com/search?q=%5Bregex%5D+phone –

+0

私は、検索、多くの結果が少しでも微調整して、そのユーザーの特定の結果に適用んでした。 – Tower

+0

*世界の電話番号フォーマットよりも多くの電話番号フォーマットがあります* –

答えて

2

このパターンは、あなたがテキストで提供した例と一致します。

((?:\d{3}[.-]|\(\d{3}\)\s+)\d{3}[.-]\d{4})(?:\s+Ext\.\s+(\d+)|\s+\(.*?\))?

あなたには、いくつかのサンプルデータを提供したり、パターンの可能性が高い向上を図ることができるように被写体が見えるものをより詳細に説明することができます。例えば、あなたのパターンが示唆しているように、すべての数字が別々の行にあるのでしょうか、それともテキストの一部ですか?


例スクリプト、およびパターンを説明:

$string = <<<EOT 
123-456-7890 

    123-456-7890 Ext. 321 
123-456-7890 Ext. 4321 
(123) 456-7890 
lorem ipsum 
123-456-7890 (Text) 
123.456.7890 
foo bar baz 
EOT; 

//preg_match_all("/((?:\d{3}[.-]|\(\d{3}\)\s+)\d{3}[.-]\d{4})(?:\s+Ext\.\s+(\d+)|\s+\(.*?\))?/i", $string, $matches); 
preg_match_all("/ 
       (      # open capturing group to hold the phone number 
        (?:     # open non-capturing group 
         \d{3}[.-]  # match 3 digits followed by a . or - 
         |    # OR, if the previous part of this group did not match 
         \(\d{3}\)\s+  # match 3 digits between parentheses folowed by one or more spaces 
        )     # close non-capturing group 
        \d{3}[.-]   # match 3 digits followed by a . or - 
        \d{4}    # match 4 digits 
       )      # close capturing group 
       (?:      # open non-capturing group 
        \s+Ext\.\s+   # one or more spaces followed by Ext. followed by one or more spaces 
        (     # open capturing group to hold the extension number 
         \d+    # match one or more digits 
        )     # close capturing group 
        |     # OR, if the previous part of this non-capturing group did not match 
        \s+\(.*?\)   # one or more spaces and then anything, except newline, between (the smallest) pair of parentheses 
       )?      # close non-capturing group, the ? makes the whole non-capturing group optional 
       /ix", $string, $matches);# the i flag makes the matches case insensitive and x allows for the comments and spacing 

echo "<pre>\n"; 
print_r($matches); 

出力:


Array 
(
    [0] => Array 
     (
      [0] => 123-456-7890 
      [1] => 123-456-7890 Ext. 321 
      [2] => 123-456-7890 Ext. 4321 
      [3] => (123) 456-7890 
      [4] => 123-456-7890 (Text) 
      [5] => 123.456.7890 
     ) 

    [1] => Array 
     (
      [0] => 123-456-7890 
      [1] => 123-456-7890 
      [2] => 123-456-7890 
      [3] => (123) 456-7890 
      [4] => 123-456-7890 
      [5] => 123.456.7890 
     ) 

    [2] => Array 
     (
      [0] => 
      [1] => 321 
      [2] => 4321 
      [3] => 
      [4] => 
      [5] => 
     ) 

) 
と他のラインのために、パターン上にも1つに一致しますが、ここにそれが必要な場合は

$string = "123-456-7890 (Text & Special Chars.); 123-456-7890 (Text)"; 

preg_match_all("/(\d{3}[.-]\d{3}[.-]\d{4})\s+\(.*\);\s+(\d{3}[.-]\d{3}[.-]\d{4})\s+\(.*?\)/i", $string, $matches); 

echo "<pre>\n"; 
print_r($matches); 

出力:

Array 
(
    [0] => Array 
     (
      [0] => 123-456-7890 (Text & Special Chars.); 123-456-7890 (Text) 
     ) 

    [1] => Array 
     (
      [0] => 123-456-7890 
     ) 

    [2] => Array 
     (
      [0] => 123-456-7890 
     ) 

) 
+0

これは私が必要としていたものです。 – Tower

関連する問題