衡阳派盒市场营销有限公司

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

您的位置:電子發燒友網>源碼下載>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%

      發表評論

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

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

      ?
      带有百家乐官网的棋牌游戏有哪些| 百家乐官网视频游戏冲值| 澳门百家乐官网破解方法| 百家乐官网出千桌| 百家乐投注综合分析法| 综合百家乐博彩论坛| bet365官网bet365gwylc| 澳门百家乐官网娱乐城送彩金| 免费百家乐官网追号| 百樂坊百家乐的玩法技巧和规则 | 百家乐中P代表| 威尼斯人娱乐百利宫| 百家乐官网案件讯问| 百家乐官网娱乐城博彩通博彩网| 金海岸百家乐的玩法技巧和规则 | 怎么玩百家乐能赢钱| 大发888赢速通充值| 百家乐官网娱乐城有几家| 百家乐的必赢方法| 德州扑克几副牌| 百家乐官网龙虎台布价格| 百家乐l23| 在线扎金花| 2024年九运| 真人百家乐官网视频| 威尼斯人娱乐城赌博网站| 金沙百家乐官网娱乐城场| 网上百家乐骗局| 鱼台县| 香港六合彩报| 百家乐官网欧洲赔率| 威尼斯人娱乐城游戏lm0| 百家乐官网投注程式| 百家乐透明发牌靴| 开心8百家乐官网现金网| 百家乐赢谷输缩| 百家乐官网如何写路| 澳门百家乐网上直赌| 网上百家乐官网危险| 百家乐三跳| 在百家乐官网二庄两闲揽的概率|