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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### [`Pattern`和`Matcher`](https://lingcoder.gitee.io/onjava8/#/book/18-Strings?id=pattern-%e5%92%8c-matcher) 通常,比起功能有限的`String`類,我們更愿意構造功能強大的正則表達式對象。只需導入`java.util.regex`包,然后用`static Pattern.compile()`方法來編譯你的正則表達式即可。它會根據你的`String`類型的正則表達式生成一個`Pattern`對象。接下來,把你想要檢索的字符串傳入`Pattern`對象的`matcher()`方法。`matcher()`方法會生成一個`Matcher`對象,它有很多功能可用(可以參考`java.util.regext.Matcher`的 JDK 文檔)。例如,它的`replaceAll()`方法能將所有匹配的部分都替換成你傳入的參數。 作為第一個示例,下面的類可以用來測試正則表達式,看看它們能否匹配一個輸入字符串。第一個控制臺參數是將要用來搜索匹配的輸入字符串,后面的一個或多個參數都是正則表達式,它們將被用來在輸入的第一個字符串中查找匹配。在Unix/Linux上,命令行中的正則表達式必須用引號括起來。這個程序在測試正則表達式時很有用,特別是當你想驗證它們是否具備你所期待的匹配功能的時候。[^3](https://lingcoder.gitee.io/onjava8/#/%E7%BD%91%E4%B8%8A%E8%BF%98%E6%9C%89%E5%BE%88%E5%A4%9A%E5%AE%9E%E7%94%A8%E5%B9%B6%E4%B8%94%E6%88%90%E7%86%9F%E7%9A%84%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%B7%A5%E5%85%B7%E3%80%82) ~~~ // strings/TestRegularExpression.java // Simple regular expression demonstration // {java TestRegularExpression // abcabcabcdefabc "abc+" "(abc)+" } import java.util.regex.*; public class TestRegularExpression { public static void main(String[] args) { if(args.length < 2) { System.out.println( "Usage:\njava TestRegularExpression " + "characterSequence regularExpression+"); System.exit(0); } System.out.println("Input: \"" + args[0] + "\""); for(String arg : args) { System.out.println( "Regular expression: \"" + arg + "\""); Pattern p = Pattern.compile(arg); Matcher m = p.matcher(args[0]); while(m.find()) { System.out.println( "Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1)); } } } } /* Output: Input: "abcabcabcdefabc" Regular expression: "abcabcabcdefabc" Match "abcabcabcdefabc" at positions 0-14 Regular expression: "abc+" Match "abc" at positions 0-2 Match "abc" at positions 3-5 Match "abc" at positions 6-8 Match "abc" at positions 12-14 Regular expression: "(abc)+" Match "abcabcabc" at positions 0-8 Match "abc" at positions 12-14 */ ~~~ 還可以在控制臺參數中加入`“(abc){2,}”`,看看執行結果。 `Pattern`對象表示編譯后的正則表達式。從這個例子可以看到,我們使用已編譯的`Pattern`對象上的`matcher()`方法,加上一個輸入字符串,從而共同構造了一個`Matcher`對象。同時,`Pattern`類還提供了一個`static`方法: ~~~ static boolean matches(String regex, CharSequence input) ~~~ 該方法用以檢查`regex`是否匹配整個`CharSequence`類型的`input`參數。編譯后的`Pattern`對象還提供了`split()`方法,它從匹配了`regex`的地方分割輸入字符串,返回分割后的子字符串`String`數組。 通過調用`Pattern.matcher()`方法,并傳入一個字符串參數,我們得到了一個`Matcher`對象。使用`Matcher`上的方法,我們將能夠判斷各種不同類型的匹配是否成功: ~~~ boolean matches() boolean lookingAt() boolean find() boolean find(int start) ~~~ 其中的`matches()`方法用來判斷整個輸入字符串是否匹配正則表達式模式,而`lookingAt()`則用來判斷該字符串(不必是整個字符串)的起始部分是否能夠匹配模式。
                  <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>

                              哎呀哎呀视频在线观看