2016-05-02 33 views
-3

私は "^ [(\ + [0-9] {1,3} \。[0-9] {4,14}(?: x。+)?] $" regex 。電話番号を検証するために、私はそれが同様に国際電話番号のために仕事をしたい それはパターンのために働いている: + 4454475294x364電話番号の正規表現を作成

私はスペースを追加したいと「 - 」も 例:+44 544から75294。 X364。私は私の正規表現で、より必要どのような変更

感謝。

+0

北米と他の国の電話機の検証を区別する必要があります。歴史的な理由により、電話番号が異なります。 – MaxZoom

答えて

1

説明

あなたは、一致させたい数字の次の例を提供しました。

+4454475294x364 
+44 544-75294 x364 
(123) 555-1212x4567 
123-555-1232 

正規表現

この正規表現は、次の操作を行います:あなたは電話番号場合

  • マッチ北米番号
  • を提供する形式の

    • マッチ国際番号を続いて拡張子をつけて、それをキャプチャします。
    • これは、あなたの質問

    ^(?:[+][0-9]{2}\s?[0-9]{3}[-]?[0-9]{3,}|(?:[(][0-9]{3}[)]|[0-9]{3})\s*[-]?\s*[0-9]{3}[-][0-9]{4})(?:\s*x\s*[0-9]+)?

    注記に記載されているだけの形式に制限されているスペース、ハイフン、および明白なスポット

  • で括弧を許可する:Javaのためにあなたがエスケープする必要がありますスラッシュ\\\のようになります。

    説明

    Regular expression visualization

    NODE      EXPLANATION 
    ---------------------------------------------------------------------- 
    ^      the beginning of a "line" 
    ---------------------------------------------------------------------- 
        (?:      group, but do not capture: 
    ---------------------------------------------------------------------- 
        [+]      any character of: '+' 
    ---------------------------------------------------------------------- 
        [0-9]{2}     any character of: '0' to '9' (2 times) 
    ---------------------------------------------------------------------- 
        \s?      whitespace (\n, \r, \t, \f, and " ") 
              (optional (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        [0-9]{3}     any character of: '0' to '9' (3 times) 
    ---------------------------------------------------------------------- 
        [-]?      any character of: '-' (optional 
              (matching the most amount possible)) 
    ---------------------------------------------------------------------- 
        [0-9]{3,}    any character of: '0' to '9' (at least 3 
              times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
        (?:      group, but do not capture: 
    ---------------------------------------------------------------------- 
         [(]      any character of: '(' 
    ---------------------------------------------------------------------- 
         [0-9]{3}     any character of: '0' to '9' (3 times) 
    ---------------------------------------------------------------------- 
         [)]      any character of: ')' 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
         [0-9]{3}     any character of: '0' to '9' (3 times) 
    ---------------------------------------------------------------------- 
        )      end of grouping 
    ---------------------------------------------------------------------- 
        \s*      whitespace (\n, \r, \t, \f, and " ") (0 
              or more times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        [-]?      any character of: '-' (optional 
              (matching the most amount possible)) 
    ---------------------------------------------------------------------- 
        \s*      whitespace (\n, \r, \t, \f, and " ") (0 
              or more times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        [0-9]{3}     any character of: '0' to '9' (3 times) 
    ---------------------------------------------------------------------- 
        [-]      any character of: '-' 
    ---------------------------------------------------------------------- 
        [0-9]{4}     any character of: '0' to '9' (4 times) 
    ---------------------------------------------------------------------- 
    )      end of grouping 
    ---------------------------------------------------------------------- 
        (?:      group, but do not capture (optional 
              (matching the most amount possible)): 
    ---------------------------------------------------------------------- 
        \s*      whitespace (\n, \r, \t, \f, and " ") (0 
              or more times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        x      'x' 
    ---------------------------------------------------------------------- 
        \s*      whitespace (\n, \r, \t, \f, and " ") (0 
              or more times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
        [0-9]+     any character of: '0' to '9' (1 or more 
              times (matching the most amount 
              possible)) 
    ---------------------------------------------------------------------- 
    )?      end of grouping 
    

    として

    マッチ上記のサンプルテキストを使用

    [0][0] = +4454475294x364 
    [1][0] = +44 544-75294 x364 
    [2][0] = (123) 555-1212x4567 
    [3][0] = 123-555-1232 
    
  • 関連する問題