<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## Java專題七:正則表達式 [TOC] ### 7.1.String類中的正則方法 ``` // java.lang.String public String[] split(String regex) { return split(regex, 0); } public String[] split(String regex, int limit) { // ... return Pattern.compile(regex).split(this, limit); } public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } public String replaceFirst(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceFirst(replacement); } public boolean matches(String regex) { return Pattern.matches(regex, this); } ``` ### 7.2.Pattern類與Matcher類 - input:輸入字符串 - regex:正則表達式 - pattern:編譯后的正則表達式 首先通過Pattern的靜態方法`public static Pattern compile(String regex)`將regex進行編譯成pattern,然后通過Matcher的`Matcher matcher(CharSequence input)`構造Matcher對象,調用Matcher對象的一些方法能實現對字符串的搜索、查找、替換。 **Pattern方法**: | 方法 | 說明 | | ------------ | ------------ | | public static Pattern compile(String regex) | 編譯regex成pattern | | public static Pattern compile(String regex, int flags) | 編譯regex成pattern,并攜帶參數 | | public String[] split(String regex) | 使用pattern分割輸入字符串input | | public String[] split(CharSequence input, int limit) | 使用pattern分割輸入字符串input,參數詳細見下面limit說明 | | public Matcher matcher(CharSequence input) | 構造一個Matcher對象 | - **Pattern#compile**方法flags說明: | 語句 | 解釋及內嵌語法 | | --- | --- | | Pattern.UNIX_LINES| -| | Pattern.CASE_INSENSITIVE | 不區分大小寫| | Pattern.COMMENTS| 以#開頭到行尾的字符被忽略| | Pattern.MULTILINE| 匹配多行| | Pattern.LITERAL| 不進行轉義字符轉義 | | Pattern.DOTALL| 匹配任意字符,包括換行符 | | Pattern.UNICODE_CASE| - | | Pattern.CANON_EQ| - | - **Pattern#split**方法limit說明: 假設input按照pattern能分成n份,如果limit=0,則分割成n份,如果limit < n,則分成limit份,如過limit > n, 則分成n份,具體如下: String input = "1123435345@qq.com"; | 語句 | 結果 | | ------------ | ------------ | | input.split("3") | [112, 4, 5, 45@qq.com] | | input.split("3", 1) | [1123435345@qq.com]| | input.split("3", 2) | [112, 435345@qq.com]| | input.split("3", 5) | [112, 4, 5, 45@qq.com] | **Matcher方法**: | 方法 | 說明 | | ------------ | ------------ | | public boolean matches() | 判斷pattern是否完全匹配input所有字符,匹配返回true | | public String replaceAll(String replacement) | 替換所有input中匹配到pattern的字符串 | | public String replaceFirst(String replacement) | 替換input中第一次匹配到pattern的字符串 | **Pattern與Matcher結合使用方法**: ``` public class RegTest { public static void main(String[] args) { String source = "1123435345@qq.com"; Pattern pattern = Pattern.compile("^([\\d]+)@(.+?[.].+)$"); Matcher matcher = pattern.matcher(source); while ( matcher.find() ) { String email = matcher.group(0); String account = matcher.group(1); String domain = matcher.group(2); System.out.println(email); System.out.println(account); System.out.println(domain); System.out.println(matcher.group(3)); } } } ``` 輸出結果如下: ``` 1123435345@qq.com 1123435345 qq.com Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 3 at java.util.regex.Matcher.group(Matcher.java:538) at java0.RegTest.main(RegTest.java:19) ``` ### 7.3.常見[正則表達式](http://www.regular-expressions.info/)語法 <table> <tr> <td>類型</td> <td>字符</td> <td>解釋</td> </tr> <tr> <td bgcolor=#ffc7ce>bound</td> <td bgcolor=#ffc7ce>^</td> <td bgcolor=#ffc7ce>匹配字符串開始標志</td> </tr> <tr> <td bgcolor=#ffc7ce>bound</td> <td bgcolor=#ffc7ce>$</td> <td bgcolor=#ffc7ce>匹配字符串結尾標志</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce>. </td> <td bgcolor=#c6efce>匹配任一字符</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce>x|y </td> <td bgcolor=#c6efce>匹配x字符或y字符,通常使用為(x|y)</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> [xyz] </td> <td bgcolor=#c6efce>匹配xyz中的任一字符</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> [^xyz] </td> <td bgcolor=#c6efce>匹配不包含xyz中的任一字符</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> [a-z] </td> <td bgcolor=#c6efce> 匹配(a,z)范圍內的任一字符</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce>[^a-z] </td> <td bgcolor=#c6efce> 匹配不在(a,z)范圍內的任一字符</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> \d </td> <td bgcolor=#c6efce> 匹配數字字符,相當于[0-9]</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> \D </td> <td bgcolor=#c6efce> 匹配非數字字符,相當于[^0-9]</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> \t </td> <td bgcolor=#c6efce> 匹配制表符</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> \s </td> <td bgcolor=#c6efce> 匹配空白字符</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> \S </td> <td bgcolor=#c6efce> 匹配非空白字符</td> </tr> <tr> <td bgcolor=#c6efce>char</td> <td bgcolor=#c6efce> \n </td> <td bgcolor=#c6efce> 匹配換行符</td> </tr> <tr> <td bgcolor=#ffeb9c>times</td> <td bgcolor=#ffeb9c> * </td> <td bgcolor=#ffeb9c> 匹配零次或多次字符或字符串組合</td> </tr> <tr> <td bgcolor=#ffeb9c>times</td> <td bgcolor=#ffeb9c> ? </td> <td bgcolor=#ffeb9c> 匹配零次或一次字符或字符串組合</td> </tr> <tr> <td bgcolor=#ffeb9c>times</td> <td bgcolor=#ffeb9c> + </td> <td bgcolor=#ffeb9c> 匹配零次或多次字符或字符串組合</td> </tr> <tr> <td bgcolor=#ffeb9c>times</td> <td bgcolor=#ffeb9c> {m} </td> <td bgcolor=#ffeb9c> 匹配m次字符或字符串組合</td> </tr> <tr> <td bgcolor=#ffeb9c>times</td> <td bgcolor=#ffeb9c> {m,} </td> <td bgcolor=#ffeb9c> 匹配至少m次字符或字符串組合</td> </tr> <tr> <td bgcolor=#ffeb9c>times</td> <td bgcolor=#ffeb9c> {m,n} </td> <td bgcolor=#ffeb9c> 匹配至少m次,至多n次字符或字符串組合</td> </tr> <tr> <td bgcolor=#00b0f0>advanced</td> <td bgcolor=#00b0f0> ? </td> <td bgcolor=#00b0f0> 緊跟在類型為times的后面,如+?,為非貪心的,匹配盡可能短的字符或字符串組合</td> </tr> <tr> <td bgcolor=#00b0f0>advanced</td> <td bgcolor=#00b0f0> (pattern) </td> <td bgcolor=#00b0f0> 匹配pattern并捕獲組,能通過group查找子表達式</td> </tr> <tr> <td bgcolor=#00b0f0>advanced</td> <td bgcolor=#00b0f0> (?:pattern) </td> <td bgcolor=#00b0f0> 匹配pattern但不捕獲組</td> </tr> </table>
                  <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>

                              哎呀哎呀视频在线观看