<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之旅 廣告
                ### [替換操作](https://lingcoder.gitee.io/onjava8/#/book/18-Strings?id=%e6%9b%bf%e6%8d%a2%e6%93%8d%e4%bd%9c) 正則表達式在進行文本替換時特別方便,它提供了許多方法: * `replaceFirst(String replacement)`以參數字符串`replacement`替換掉第一個匹配成功的部分。 * `replaceAll(String replacement)`以參數字符串`replacement`替換所有匹配成功的部分。 * `appendReplacement(StringBuffer sbuf, String replacement)`執行漸進式的替換,而不是像`replaceFirst()`和`replaceAll()`那樣只替換第一個匹配或全部匹配。這是一個非常重要的方法。它允許你調用其他方法來生成或處理`replacement`(`replaceFirst()`和`replaceAll()`則只能使用一個固定的字符串),使你能夠以編程的方式將目標分割成組,從而具備更強大的替換功能。 * `appendTail(StringBuffer sbuf)`在執行了一次或多次`appendReplacement()`之后,調用此方法可以將輸入字符串余下的部分復制到`sbuf`中。 下面的程序演示了如何使用這些替換方法。開頭部分注釋掉的文本,就是正則表達式要處理的輸入字符串: ~~~ // strings/TheReplacements.java import java.util.regex.*; import java.nio.file.*; import java.util.stream.*; /*! Here's a block of text to use as input to the regular expression matcher. Note that we first extract the block of text by looking for the special delimiters, then process the extracted block. !*/ public class TheReplacements { public static void main(String[] args) throws Exception { String s = Files.lines( Paths.get("TheReplacements.java")) .collect(Collectors.joining("\n")); // Match specially commented block of text above: Matcher mInput = Pattern.compile( "/\\*!(.*)!\\*/", Pattern.DOTALL).matcher(s); if(mInput.find()) s = mInput.group(1); // Captured by parentheses // Replace two or more spaces with a single space: s = s.replaceAll(" {2,}", " "); // Replace 1+ spaces at the beginning of each // line with no spaces. Must enable MULTILINE mode: s = s.replaceAll("(?m)^ +", ""); System.out.println(s); s = s.replaceFirst("[aeiou]", "(VOWEL1)"); StringBuffer sbuf = new StringBuffer(); Pattern p = Pattern.compile("[aeiou]"); Matcher m = p.matcher(s); // Process the find information as you // perform the replacements: while(m.find()) m.appendReplacement(sbuf, m.group().toUpperCase()); // Put in the remainder of the text: m.appendTail(sbuf); System.out.println(sbuf); } } /* Output: Here's a block of text to use as input to the regular expression matcher. Note that we first extract the block of text by looking for the special delimiters, then process the extracted block. H(VOWEL1)rE's A blOck Of tExt tO UsE As InpUt tO thE rEgUlAr ExprEssIOn mAtchEr. NOtE thAt wE fIrst ExtrAct thE blOck Of tExt by lOOkIng fOr thE spEcIAl dElImItErs, thEn prOcEss thE ExtrActEd blOck. */ ~~~ 此處使用上一章介紹過的[`Files`](https://lingcoder.gitee.io/onjava8/#/./17-Files)類打開并讀入文件。`Files.lines()`返回一個`Stream`對象,包含讀入的所有行,`Collectors.joining()`在每一行的結尾追加參數字符序列,最終拼接成一個`String`對象。 `mInput`匹配`/*!`和`!*/`之間的所有文字(注意分組的括號)。接下來,將存在兩個或兩個以上空格的地方,縮減為一個空格,并且刪除每行開頭部分的所有空格(為了使每一行都達到這個效果,而不僅僅是刪除文本開頭部分的空格,這里特意開啟了多行模式)。這兩個替換操作所使用的的`replaceAll()`是`String`對象自帶的方法,在這里,使用此方法更方便。注意,因為這兩個替換操作都只使用了一次`replaceAll()`,所以,與其編譯為`Pattern`,不如直接使用`String`的`replaceAll()`方法,而且開銷也更小些。 `replaceFirst()`只對找到的第一個匹配進行替換。此外,`replaceFirst()`和`replaceAll()`方法用來替換的只是普通字符串,所以,如果想對這些替換字符串進行某些特殊處理,這兩個方法時無法勝任的。如果你想要那么做,就應該使用`appendReplacement()`方法。該方法允許你在執行替換的過程中,操作用來替換的字符串。在這個例子中,先構造了`sbuf`用來保存最終結果,然后用`group()`選擇一個組,并對其進行處理,將正則表達式找到的元音字母替換成大些字母。一般情況下,你應該遍歷執行所有的替換操作,然后再調用`appendTail()`方法,但是,如果你想模擬`replaceFirst()`(或替換n次)的行為,那就只需要執行一次替換,然后調用`appendTail()`方法,將剩余未處理的部分存入`sbuf`即可。 同時,`appendReplacement()`方法還允許你通過`\$g`直接找到匹配的某個組,這里的`g`就是組號。然而,它只能應付一些簡單的處理,無法實現類似前面這個例子中的功能。
                  <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>

                              哎呀哎呀视频在线观看