吴忠躺衫网络科技有限公司

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

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

      發表評論

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

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

      ?
      大发888娱乐城充值lm0| 百家乐官网规则| 顶尖百家乐开户| 百家乐长龙技巧| 大发888网页版登录| 新巴尔虎左旗| 百家乐官网小九梭哈| 百家乐长庄投注| 六合彩网页| 威尼斯人娱乐城最新网址| 香港百家乐娱乐场开户注册| 百家乐官网投注窍门| 百家乐真人娱乐场开户注册 | 威尼斯人娱乐城购物| 钱隆百家乐智能| 百家乐游戏打水| OK娱乐城| 迪威百家乐官网娱乐| 大发888娱乐城注册送筹码| 辽中县| 风水24山组成| 大发888我发财| 百家乐官网半圆桌| 百家乐游戏下裁| 大发888下载安全的| 网络百家乐官网游赌博| 百家乐分析仪有真的吗| 百家乐开发| 现金百家乐| 足球投注技巧| 网络百家乐官网赚| 大发888网页多少| 百家乐官网庄6点| 威尼斯人娱乐城位置| 百家乐官网投注方法网| 百家乐娱乐备用网址| 求购百家乐官网程序| 海立方百家乐海立方| 沙龙娱乐| 做生意的风水朝向| 大发888注册就送58|