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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 限制文本中的行數的 Java 正則表達式 > 原文: [https://howtodoinjava.com/regex/java-regex-validate-limit-the-number-of-lines-in-text/](https://howtodoinjava.com/regex/java-regex-validate-limit-the-number-of-lines-in-text/) 在此 Java 正則表達式教程中,我們將學習測試輸入文本中的行數是否在某個最小和最大限制之間,而不考慮字符串中出現了多少個總字符。 用于匹配行數的正則表達式將取決于用作行分隔符的確切字符或字符序列。 實際上,行分隔符可能會根據操作系統的約定,應用程序或用戶首選項等而有所不同。 因此,編寫理想的解決方案取決于應支持哪些約定來指示新行的開始。 本教程中討論的以下解決方案支持標準 MS-DOS/Windows(“`\r\n`”),舊版 MacOS(“`\r`”)和 Unix/Linux/BSD/OSX(“`\n`”)行中斷約定。 > **正則表達式:`\\A(?>[^\r\n]*(?>\r\n?|\n)){0,3}[^\r\n]*\\z`** ## 正則表達式的說明 ```java \A # Assert position at the beginning of the string. (?> # Group but don't capture or keep backtracking positions: [^\r\n]* # Match zero or more characters except CR and LF. (?> # Group but don't capture or keep backtracking positions: \r\n? # Match a CR, with an optional following LF (CRLF). | # Or: \n # Match a standalone LF character. ) # End the noncapturing, atomic group. ){0,4} # End group; repeat between zero and four times. [^\r\n]* # Match zero or more characters except CR and LF. \z # Assert position at the end of the string. CR : Carriage Return (\r\n) LF : Line Feed (\n) ``` 在正則表達式之上,驗證內容具有最少零行和最多三行。 讓我們驗證解決方案正則表達式。 #### 零行驗證 ```java StringBuilder builder = new StringBuilder(); String regex = "\\A(?>[^\r\n]*(?>\r\n?|\n)){0,3}[^\r\n]*\\z"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(builder.toString()); System.out.println(matcher.matches()); Output : true ``` #### 兩行驗證 ```java StringBuilder builder = new StringBuilder(); builder.append("Test Line 1"); builder.append("\n"); builder.append("Test Line 2"); builder.append("\n"); String regex = "\\A(?>[^\r\n]*(?>\r\n?|\n)){0,3}[^\r\n]*\\z"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(builder.toString()); System.out.println(matcher.matches()); Output : true ``` #### 六行驗證 ```java StringBuilder builder = new StringBuilder(); builder.append("Test Line 1"); builder.append("\n"); builder.append("Test Line 2"); builder.append("\n"); builder.append("Test Line 3"); builder.append("\n"); builder.append("Test Line 4"); builder.append("\n"); builder.append("Test Line 5"); builder.append("\n"); builder.append("Test Line 6"); builder.append("\n"); String regex = "\\A(?>[^\r\n]*(?>\r\n?|\n)){0,3}[^\r\n]*\\z"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(builder.toString()); System.out.println(matcher.matches()); Output : 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>

                              哎呀哎呀视频在线观看