c++ 正则表达式_Java入门 - 语言基础 - 18.正则表达式
1.概述
正則表達式定義了字符串的模式。
正則表達式可以用來搜索、編輯或處理文本。
正則表達式并不僅限于某一種語言,但是在每種語言中有細微的差別。
正則表達式實例
一個字符串其實就是一個簡單的正則表達式,例如 Hello World 正則表達式匹配 "Hello World" 字符串。
.(點號)也是一個正則表達式,它匹配任何一個字符如:"a" 或 "1"。
下表列出了一些正則表達式的實例及描述:
正則表達式實例
Java 正則表達式和 Perl 的是最為相似的。
java.util.regex 包主要包括以下三個類:
- Pattern 類:pattern 對象是一個正則表達式的編譯表示。Pattern 類沒有公共構造方法。要創(chuàng)建一個 Pattern 對象,你必須首先調(diào)用其公共靜態(tài)編譯方法,它返回一個 Pattern 對象。該方法接受一個正則表達式作為它的第一個參數(shù)。
- Matcher 類:Matcher 對象是對輸入字符串進行解釋和匹配操作的引擎。與 Pattern 類一樣,Matcher 也沒有公共構造方法。你需要調(diào)用 Pattern 對象的 matcher 方法來獲得一個 Matcher 對象。
- PatternSyntaxException 類:PatternSyntaxException 是一個非強制異常類,它表示一個正則表達式模式中的語法錯誤。
以下實例中使用了正則表達式 .*work100.* 用于查找字符串中是否包了 work100 子串:
import java.util.regex.*; class RegexExample1{ public static void main(String[] args){ String content = "I am xiaojun " + "from work100.net."; String pattern = ".*work100.*"; boolean isMatch = Pattern.matches(pattern, content); System.out.println("字符串中是否包含了 'work100' 子字符串? " + isMatch); }}實例輸出結果為:
字符串中是否包含了 'work100' 子字符串? true2.捕獲組
捕獲組是把多個字符當一個單獨單元進行處理的方法,它通過對括號內(nèi)的字符分組來創(chuàng)建。
例如,正則表達式 (dog) 創(chuàng)建了單一分組,組里包含"d","o",和"g"。
捕獲組是通過從左至右計算其開括號來編號。例如,在表達式((A)(B(C))),有四個這樣的組:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
可以通過調(diào)用 matcher 對象的 groupCount 方法來查看表達式有多少個分組。groupCount 方法返回一個 int 值,表示 matcher 對象當前有多個捕獲組。
還有一個特殊的組(group(0)),它總是代表整個表達式。該組不包括在 groupCount 的返回值中。
實例
下面的例子說明如何從一個給定的字符串中找到數(shù)字串:
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ public static void main( String[] args ){ // 按指定模式在字符串查找 String line = "This order was placed for QT3000! OK?"; String pattern = "(D*)(d+)(.*)"; // 創(chuàng)建 Pattern 對象 Pattern r = Pattern.compile(pattern); // 現(xiàn)在創(chuàng)建 matcher 對象 Matcher m = r.matcher(line); if (m.find( )) { System.out.println("Found value: " + m.group(0) ); System.out.println("Found value: " + m.group(1) ); System.out.println("Found value: " + m.group(2) ); System.out.println("Found value: " + m.group(3) ); } else { System.out.println("NO MATCH"); } }}以上實例編譯運行結果如下:
Found value: This order was placed for QT3000! OK?Found value: This order was placed for QTFound value: 3000Found value: ! OK?3.正則表達式語法
在其他語言中, 表示:我想要在正則表達式中插入一個普通的(字面上的)反斜杠,請不要給它任何特殊的意義。
在 Java 中, 表示:我要插入一個正則表達式的反斜線,所以其后的字符具有特殊的意義。
所以,在其他的語言中(如Perl),一個反斜杠 就足以具有轉(zhuǎn)義的作用,而在 Java 中正則表達式中則需要有兩個反斜杠才能被解析為其他語言中的轉(zhuǎn)義作用。也可以簡單的理解在 Java 的正則表達式中,兩個 代表其他語言中的一個 ,這也就是為什么表示一位數(shù)字的正則表達式是 d,而表示一個普通的反斜杠是 。
語法-1
語法-2
語法-3
語法-4
根據(jù) Java Language Specification 的要求,Java 源代碼的字符串中的反斜線被解釋為 Unicode 轉(zhuǎn)義或其他字符轉(zhuǎn)義。因此必須在字符串字面值中使用兩個反斜線,表示正則表達式受到保護,不被 Java 字節(jié)碼編譯器解釋。例如,當解釋為正則表達式時,字符串字面值 "b" 與單個退格字符匹配,而 "b" 與單詞邊界匹配。字符串字面值 "(hello)" 是非法的,將導致編譯時錯誤;要與字符串 (hello) 匹配,必須使用字符串字面值 "(hello)"。
4.Matcher類的方法
索引方法
索引方法提供了有用的索引值,精確表明輸入字符串中在哪能找到匹配:
索引方法
研究方法
研究方法用來檢查輸入字符串并返回一個布爾值,表示是否找到該模式:
研究方法
替換方法
替換方法是替換輸入字符串里文本的方法:
替換方法
start和end方法
下面是一個對單詞 "cat" 出現(xiàn)在輸入字符串中出現(xiàn)次數(shù)進行計數(shù)的例子:
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ private static final String REGEX = "bcatb"; private static final String INPUT = "cat cat cat cattie cat"; public static void main(String[] args){ Pattern p = Pattern.compile(REGEX); Matcher m = p.matcher(INPUT); // 獲取 matcher 對象 int count = 0; while(m.find()) { count++; System.out.println("Match number "+count); System.out.println("start(): "+m.start()); System.out.println("end(): "+m.end()); } }}以上實例編譯運行結果如下:
Match number 1start(): 0end(): 3Match number 2start(): 4end(): 7Match number 3start(): 8end(): 11Match number 4start(): 19end(): 22可以看到這個例子是使用單詞邊界,以確保字母 "c" "a" "t" 并非僅是一個較長的詞的子串。它也提供了一些關于輸入字符串中匹配發(fā)生位置的有用信息。
Start 方法返回在以前的匹配操作期間,由給定組所捕獲的子序列的初始索引,end 方法最后一個匹配字符的索引加 1。
matches和lookingAt方法
matches 和 lookingAt 方法都用來嘗試匹配一個輸入序列模式。它們的不同是 matches 要求整個序列都匹配,而 lookingAt 不要求。
lookingAt 方法雖然不需要整句都匹配,但是需要從第一個字符開始匹配。
這兩個方法經(jīng)常在輸入字符串的開始使用。
我們通過下面這個例子,來解釋這個功能:
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ private static final String REGEX = "foo"; private static final String INPUT = "fooooooooooooooooo"; private static final String INPUT2 = "ooooofoooooooooooo"; private static Pattern pattern; private static Matcher matcher; private static Matcher matcher2; public static void main( String[] args ){ pattern = Pattern.compile(REGEX); matcher = pattern.matcher(INPUT); matcher2 = pattern.matcher(INPUT2); System.out.println("Current REGEX is: "+REGEX); System.out.println("Current INPUT is: "+INPUT); System.out.println("Current INPUT2 is: "+INPUT2); System.out.println("lookingAt(): "+matcher.lookingAt()); System.out.println("matches(): "+matcher.matches()); System.out.println("lookingAt(): "+matcher2.lookingAt()); }}以上實例編譯運行結果如下:
Current REGEX is: fooCurrent INPUT is: foooooooooooooooooCurrent INPUT2 is: ooooofoooooooooooolookingAt(): truematches(): falselookingAt(): falsereplaceFirst和replaceAll方法
replaceFirst 和 replaceAll 方法用來替換匹配正則表達式的文本。不同的是,replaceFirst 替換首次匹配,replaceAll 替換所有匹配。
下面的例子來解釋這個功能:
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ private static String REGEX = "dog"; private static String INPUT = "The dog says meow. " + "All dogs say meow."; private static String REPLACE = "cat"; public static void main(String[] args) { Pattern p = Pattern.compile(REGEX); // get a matcher object Matcher m = p.matcher(INPUT); INPUT = m.replaceAll(REPLACE); System.out.println(INPUT); }}以上實例編譯運行結果如下:
The cat says meow. All cats say meow.appendReplacement和appendTail方法
Matcher 類也提供了 appendReplacement 和 appendTail 方法用于文本替換:
看下面的例子來解釋這個功能:
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ private static String REGEX = "a*b"; private static String INPUT = "aabfooaabfooabfoobkkk"; private static String REPLACE = "-"; public static void main(String[] args) { Pattern p = Pattern.compile(REGEX); // 獲取 matcher 對象 Matcher m = p.matcher(INPUT); StringBuffer sb = new StringBuffer(); while(m.find()){ m.appendReplacement(sb,REPLACE); } m.appendTail(sb); System.out.println(sb.toString()); }以上實例編譯運行結果如下:
-foo-foo-foo-kkkPatternSyntaxException類的方法
PatternSyntaxException 是一個非強制異常類,它指示一個正則表達式模式中的語法錯誤。
PatternSyntaxException 類提供了下面的方法來幫助我們查看發(fā)生了什么錯誤。
PatternSyntaxException類提供的方法
總結
以上是生活随笔為你收集整理的c++ 正则表达式_Java入门 - 语言基础 - 18.正则表达式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA的静态方法的运算_java. u
- 下一篇: python线程安全的计数器_+ =运算