在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

您好,歡迎來電子發燒友網! ,新用戶?[免費注冊]

您的位置:電子發燒友網>源碼下載>java源碼下載>

關于java正則表達式的用法詳解

大小:1.4 MB 人氣: 2017-09-27 需要積分:2

   正則表達式

  一個正則表達式是一個用于文本搜索的文本模式。換句話說,在文本中搜索出現的模式。例如,你可以用正則表達式搜索網頁中的郵箱地址或超鏈接。

  正則表達式示例

  下面是一個簡單的Java正則表達式的例子,用于在文本中搜索 http://

  Stringtext= “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;Stringpattern = “.*http://.*”; booleanmatches = Pattern.matches(pattern, text); System.out.println( “matches = ”+ matches);

  示例代碼實際上沒有檢測找到的 http:// 是否是一個合法超鏈接的一部分,如包含域名和后綴(.com,.net 等等)。代碼只是簡單的查找字符串 http:// 是否出現。

  Java6 中關于正則表達式的API

  本教程介紹了Java6 中關于正則表達式的API。

  Pattern (java.util.regex.Pattern)

  類 java.util.regex.Pattern 簡稱 Pattern, 是Java正則表達式API中的主要入口,無論何時,需要使用正則表達式,從Pattern 類開始

  Pattern.matches()

  檢查一個正則表達式的模式是否匹配一段文本的最直接方法是調用靜態方法Pattern.matches(),示例如下:

  Stringtext= “This is the text to be searched ”+ “for occurrences of the pattern.”;Stringpattern = “.*is.*”; booleanmatches = Pattern.matches(pattern, text); System.out.println( “matches = ”+ matches);

  上面代碼在變量 text 中查找單詞 “is” 是否出現,允許”is” 前后包含 0或多個字符(由 .* 指定)

  Pattern.matches() 方法適用于檢查 一個模式在一個文本中出現一次的情況,或適用于Pattern類的默認設置。

  如果需要匹配多次出現,甚至輸出不同的匹配文本,或者只是需要非默認設置。需要通過Pattern.compile() 方法得到一個Pattern 實例。

  Pattern.compile()

  如果需要匹配一個正則表達式在文本中多次出現,需要通過Pattern.compile() 方法創建一個Pattern對象。示例如下

  Stringtext = “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;StringpatternString = “.*http://.*”; Patternpattern = Pattern.compile(patternString);

  可以在Compile 方法中,指定一個特殊標志:

  Patternpattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);

  Pattern 類包含多個標志(int 類型),這些標志可以控制Pattern 匹配模式的方式。上面代碼中的標志使模式匹配是忽略大小寫

  Pattern.matcher()

  一旦獲得了Pattern對象,接著可以獲得Matcher對象。Matcher 示例用于匹配文本中的模式。示例如下

  Matcher matcher = pattern.matcher(text);

  Matcher類有一個matches()方法,可以檢查文本是否匹配模式。以下是關于Matcher的一個完整例子

  String text = “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;String patternString = “.*http://.*”;Pattern pattern = Pattern .compile(patternString, Pattern .CASE_INSENSITIVE) ;Matcher matcher = pattern .matcher(text) ;boolean matches = matcher .matches() ;System .out.println( “matches = ”+ matches) ;

  Pattern.split()

  Pattern 類的 split()方法,可以用正則表達式作為分隔符,把文本分割為String類型的數組。示例:

  Stringtext = “A sep Text sep With sep Many sep Separators”; StringpatternString = “sep”; Pattern pattern = Pattern.compile(patternString); String[] split= pattern. split(text); System.out.println( “split.length = ”+ split.length); for( Stringelement : split){ System.out.println( “element = ”+ element); }

  上例中把text 文本分割為一個包含5個字符串的數組。

  Pattern.pattern()

  Pattern 類的 pattern 返回用于創建Pattern 對象的正則表達式,示例:

  StringpatternString = “sep”; Patternpattern = Pattern.compile(patternString);Stringpattern2 = pattern.pattern();

  上面代碼中 pattern2 值為sep ,與patternString 變量相同。

  Matcher (java.util.regex.Matcher)

  java.util.regex.Matcher 類用于匹配一段文本中多次出現一個正則表達式,Matcher 也適用于多文本中匹配同一個正則表達式。

  Matcher 有很多有用的方法,詳細請參考官方JavaDoc。這里只介紹核心方法。

  以下代碼演示如何使用Matcher

  Stringtext= “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;StringpatternString = “.*http://.*”; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher( text); booleanmatches = matcher.matches();

  首先創建一個Pattern,然后得到Matcher ,調用matches() 方法,返回true 表示模式匹配,返回false表示不匹配。

  可以用Matcher 做更多的事。

  創建Matcher

  通過Pattern 的matcher() 方法創建一個Matcher。

  Stringtext = “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;StringpatternString = “.*http://.*”; Patternpattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(text);

  matches()

  Matcher 類的 matches() 方法用于在文本中匹配正則表達式

  boolean matches= matcher. matches();

  如果文本匹配正則表達式,matches() 方法返回true。否則返回false。

  matches() 方法不能用于查找正則表達式多次出現。如果需要,請使用find(), start() 和 end() 方法。

  lookingAt()

  lookingAt() 與matches() 方法類似,最大的不同是,lookingAt()方法對文本的開頭匹配正則表達式;而

  matches() 對整個文本匹配正則表達式。換句話說,如果正則表達式匹配文本開頭而不匹配整個文本,lookingAt() 返回true,而matches() 返回false。 示例:

  String text = “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;String patternString = “This is the”;Pattern pattern = Pattern.compile(patternString, Pattern .CASE_INSENSITIVE) ;Matcher matcher = pattern.matcher(text) ;System .out.println( “lookingAt = ”+ matcher .lookingAt()) ;System.out.println( “matches = ”+ matcher .matches()) ;

非常好我支持^.^

(0) 0%

不好我反對

(0) 0%

      發表評論

      用戶評論
      評價:好評中評差評

      發表評論,獲取積分! 請遵守相關規定!

      ?
      主站蜘蛛池模板: 人人干人人爽 | 国产午夜精品福利久久 | 亚洲综合激情九月婷婷 | 91大神在线观看精品一区 | 又长又大又粗又硬3p免费视频 | 偷自在线| 国产精品乱码高清在线观看 | 国产小视频免费 | 亚洲 欧美 91 | 电影一区二区三区 | 97影院理论片手机在线观看 | 国模私拍福利一区二区 | 老色批 | 天天色天天干天天 | 夜夜夜夜夜夜夜猛噜噜噜噜噜噜 | 夜夜爽夜夜操 | 精品国产污网站在线观看15 | 在线免费观看黄色小视频 | 激情综合五月亚洲婷婷 | 久久青草精品免费资源站 | 天天综合在线视频 | 在线播放视频网站 | 全免费午夜一级毛片真人 | 视频一区在线观看 | 四虎影院最新地址 | 色多多·com| 日本超黄视频 | 91男人 | 亚洲香蕉久久 | 男女交性特一级 | 欧美三级视频网站 | 34pao强力打造免费永久视频 | 俄罗斯美女在线观看一区 | 久久综合免费视频 | 九九99久久精品影视 | 免费一级特黄特色大片在线 | 足控免费视频xxav | 天堂网在线资源 | 久久精品视频国产 | 国产你懂 | 特级淫片aaaa毛片aa视频 |