<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 驗證 SSN(社會安全號碼)的 Java 正則表達式 > 原文: [https://howtodoinjava.com/regex/java-regex-validate-social-security-numbers-ssn/](https://howtodoinjava.com/regex/java-regex-validate-social-security-numbers-ssn/) 在此 java 正則表達式教程中,我們將學習使用正則表達式來測試用戶是否在您的應用程序或網站表單中輸入了有效的社會安全號碼。 ## 有效的 SSN 編號格式 美國社會安全號碼是九位數字,格式為`AAA-GG-SSSS`,并具有以下規則。 * 前三位數字稱為區域號。 區域號不能為 000、666 或 900 到 999 之間。 * 數字 4 和 5 稱為組號,范圍從 01 到 99。 * 后四位數字是從 0001 到 9999 的序列號。 為了驗證以上 3 條規則,我們的正則表達式為: > **正則表達式:`^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$`** ## 驗證 SSN 正則表達式的說明 ```java ^ # Assert position at the beginning of the string. (?!000|666) # Assert that neither "000" nor "666" can be matched here. [0-8] # Match a digit between 0 and 8. [0-9]{2} # Match a digit, exactly two times. - # Match a literal "-". (?!00) # Assert that "00" cannot be matched here. [0-9]{2} # Match a digit, exactly two times. - # Match a literal "-". (?!0000) # Assert that "0000" cannot be matched here. [0-9]{4} # Match a digit, exactly four times. $ # Assert position at the end of the string. ``` 現在,我們使用一些演示 SSN 編號測試我們的 SSN 驗證正則表達式。 ```java List<String> ssns = new ArrayList<String>(); //Valid SSNs ssns.add("123-45-6789"); ssns.add("856-45-6789"); //Invalid SSNs ssns.add("000-45-6789"); ssns.add("666-45-6789"); ssns.add("901-45-6789"); ssns.add("85-345-6789"); ssns.add("856-453-6789"); ssns.add("856-45-67891"); ssns.add("856-456789"); String regex = "^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$"; Pattern pattern = Pattern.compile(regex); for (String number : ssns) { Matcher matcher = pattern.matcher(number); System.out.println(matcher.matches()); } Output: true true false false false false false false false ``` 我建議您使用上述簡單的正則表達式嘗試更多的變化。 **學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看