<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國際加速解決方案。 廣告
                ### [基礎](https://lingcoder.gitee.io/onjava8/#/book/18-Strings?id=%e5%9f%ba%e7%a1%80) 一般來說,正則表達式就是以某種方式來描述字符串,因此你可以說:“如果一個字符串含有這些東西,那么它就是我正在找的東西。”例如,要找一個數字,它可能有一個負號在最前面,那你就寫一個負號加上一個問號,就像這樣: ~~~ -? ~~~ 要描述一個整數,你可以說它有一位或多位阿拉伯數字。在正則表達式中,用`\d`表示一位數字。如果在其他語言中使用過正則表達式,那你可能就能發現 Java 對反斜線 \\ 的不同處理方式。在其他語言中,`\\`表示“我想要在正則表達式中插入一個普通的(字面上的)反斜線,請不要給它任何特殊的意義。”而在Java中,`\\`的意思是“我要插入一個正則表達式的反斜線,所以其后的字符具有特殊的意義。”例如,如果你想表示一位數字,那么正則表達式應該是`\\d`。如果你想插入一個普通的反斜線,應該這樣寫`\\\`。不過換行符和制表符之類的東西只需要使用單反斜線:`\n\t`。[^2](https://lingcoder.gitee.io/onjava8/#/Java%E5%B9%B6%E9%9D%9E%E5%9C%A8%E4%B8%80%E5%BC%80%E5%A7%8B%E5%B0%B1%E6%94%AF%E6%8C%81%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%8C%E5%9B%A0%E6%AD%A4%E8%BF%99%E4%B8%AA%E4%BB%A4%E4%BA%BA%E8%B4%B9%E8%A7%A3%E7%9A%84%E8%AF%AD%E6%B3%95%E6%98%AF%E7%A1%AC%E5%A1%9E%E8%BF%9B%E6%9D%A5%E7%9A%84%E3%80%82) 要表示“一個或多個之前的表達式”,應該使用`+`。所以,如果要表示“可能有一個負號,后面跟著一位或多位數字”,可以這樣: ~~~ -?\\d+ ~~~ 應用正則表達式最簡單的途徑,就是利用`String`類內建的功能。例如,你可以檢查一個`String`是否匹配如上所述的正則表達式: ~~~ // strings/IntegerMatch.java public class IntegerMatch { public static void main(String[] args) { System.out.println("-1234".matches("-?\\d+")); System.out.println("5678".matches("-?\\d+")); System.out.println("+911".matches("-?\\d+")); System.out.println("+911".matches("(-|\\+)?\\d+")); } } /* Output: true true false true */ ~~~ 前兩個字符串都滿足對應的正則表達式,匹配成功。第三個字符串以`+`開頭,這也是一個合法的符號,但與對應的正則表達式卻不匹配。因此,我們的正則表達式應該描述為:“可能以一個加號或減號開頭”。在正則表達式中,用括號將表達式進行分組,用豎線`|`表示或操作。也就是: ~~~ (-|\\+)? ~~~ 這個正則表達式表示字符串的起始字符可能是一個`-`或`+`,或者二者都沒有(因為后面跟著`?`修飾符)。因為字符`+`在正則表達式中有特殊的意義,所以必須使用`\\`將其轉義,使之成為表達式中的一個普通字符。 `String`類還自帶了一個非常有用的正則表達式工具——`split()`方法,其功能是“將字符串從正則表達式匹配的地方切開。” ~~~ // strings/Splitting.java import java.util.*; public class Splitting { public static String knights = "Then, when you have found the shrubbery, " + "you must cut down the mightiest tree in the " + "forest...with... a herring!"; public static void split(String regex) { System.out.println( Arrays.toString(knights.split(regex))); } public static void main(String[] args) { split(" "); // Doesn't have to contain regex chars split("\\W+"); // Non-word characters split("n\\W+"); // 'n' followed by non-words } } /* Output: [Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest...with..., a, herring!] [Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring] [The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest...with... a herring!] */ ~~~ 首先看第一個語句,注意這里用的是普通的字符作為正則表達式,其中并不包含任何特殊字符。因此第一個`split()`只是按空格來劃分字符串。 第二個和第三個`split()`都用到了`\\W`,它的意思是一個非單詞字符(如果 W 小寫,`\\w`,則表示一個單詞字符)。通過第二個例子可以看到,它將標點字符刪除了。第三個`split()`表示“字母`n`后面跟著一個或多個非單詞字符。”可以看到,在原始字符串中,與正則表達式匹配的部分,在最終結果中都不存在了。 `String.split()`還有一個重載的版本,它允許你限制字符串分割的次數。 用正則表達式進行替換操作時,你可以只替換第一處匹配,也可以替換所有的匹配: ~~~ // strings/Replacing.java public class Replacing { static String s = Splitting.knights; public static void main(String[] args) { System.out.println( s.replaceFirst("f\\w+", "located")); System.out.println( s.replaceAll("shrubbery|tree|herring","banana")); } } /* Output: Then, when you have located the shrubbery, you must cut down the mightiest tree in the forest...with... a herring! Then, when you have found the banana, you must cut down the mightiest banana in the forest...with... a banana! */ ~~~ 第一個表達式要匹配的是,以字母`f`開頭,后面跟一個或多個字母(注意這里的`w`是小寫的)。并且只替換掉第一個匹配的部分,所以 “found” 被替換成 “located”。 第二個表達式要匹配的是三個單詞中的任意一個,因為它們以豎線分割表示“或”,并且替換所有匹配的部分。 稍后你會看到,`String`之外的正則表達式還有更強大的替換工具,例如,可以通過方法調用執行替換。而且,如果正則表達式不是只使用一次的話,非`String`對象的正則表達式明顯具備更佳的性能。
                  <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>

                              哎呀哎呀视频在线观看