<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 北美電話號碼的 Java 正則表達式 > 原文: [https://howtodoinjava.com/regex/java-regex-validate-and-format-north-american-phone-numbers/](https://howtodoinjava.com/regex/java-regex-validate-and-format-north-american-phone-numbers/) 在此正則表達式教程中,我們將學習驗證用戶輸入的電話號碼是否為特定格式(在此示例中為北美格式),如果號碼正確,則將其重新格式化為標準格式以進行顯示。 我測試了以下格式,包括`1234567890`、`123-456-7890`、`123.456.7890`、`123 456 7890`,`(123) 456 7890`以及所有此類組合。 ## 使用正則表達式驗證北美電話號碼 > **正則表達式:`^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$`** 以上正則表達式可用于驗證電話號碼的所有格式,以檢查它們是否為有效的北美電話號碼。 ```java List phoneNumbers = new ArrayList(); phoneNumbers.add("1234567890"); phoneNumbers.add("123-456-7890"); phoneNumbers.add("123.456.7890"); phoneNumbers.add("123 456 7890"); phoneNumbers.add("(123) 456 7890"); //Invalid phone numbers phoneNumbers.add("12345678"); phoneNumbers.add("12-12-111"); String regex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$"; Pattern pattern = Pattern.compile(regex); for(String email : phoneNumbers) { Matcher matcher = pattern.matcher(email); System.out.println(email +" : "+ matcher.matches()); } Output: 1234567890 : true 123-456-7890 : true 123.456.7890 : true 123 456 7890 : true (123) 456 7890 : true 12345678 : false 12-12-111 : false ``` ## 使用正則表達式格式化北美電話號碼 > **正則表達式:`($1) $2-$3`** 使用上面的正則表達式來重新格式化在上述步驟中驗證過的電話號碼,以便以一致的方式重新格式化以存儲/顯示目的。 ```java List phoneNumbers = new ArrayList(); phoneNumbers.add("1234567890"); phoneNumbers.add("123-456-7890"); phoneNumbers.add("123.456.7890"); phoneNumbers.add("123 456 7890"); phoneNumbers.add("(123) 456 7890"); //Invalid phone numbers phoneNumbers.add("12345678"); phoneNumbers.add("12-12-111"); String regex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$"; Pattern pattern = Pattern.compile(regex); for(String email : phoneNumbers) { Matcher matcher = pattern.matcher(email); //System.out.println(email +" : "+ matcher.matches()); //If phone number is correct then format it to (123)-456-7890 if(matcher.matches()) { System.out.println(matcher.replaceFirst("($1) $2-$3")); } } Output: (123) 456-7890 (123) 456-7890 (123) 456-7890 (123) 456-7890 (123) 456-7890 ``` 上面的正則表達式也可以在 Java 腳本中工作。 因此,下次需要它們時,請務必將它們放在方便的地方。 **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看