正則表達式 實(shí)質(zhì)上是一個(gè)模式,用于描述共享該模式的一組字符串 例如:
test string
test longer string
這些字符串都是以"Test”開(kāi)頭,并以“String” 結尾
java語(yǔ)言中,通過(guò)Java language Regular Expression(或 regex)來(lái)實(shí)現正則表達式
API如下:
* Pattern,描述了一個(gè)字符串模式。
* Matcher,測試字符串,查看它是否與模式匹配。
* PatternSyntaxException,在您試著(zhù)定義模式時(shí)告訴您哪些與該模式有關(guān)的地方是不可接受的。
正則表達式語(yǔ)法介紹,常用的格式匹配如下:
- 假設你要查找包含“str” 。正則表達式:“str” , “test string” “test longer string”都能與之匹配
- 句點(diǎn)符號:“.” 任何字符 。例如正則表達式: "a.b" 匹配“a b”,"abb","abcb"
- 方括號符號:[] 表示范圍 。例如正則表示式:[1-9] 表示從1到9。[abc]表示‘a’,'b','c'中任意一個(gè)字符。[a-f]表示'a'到'f'中一個(gè)字符
- 或符號:| 表示或者。 例如正則表達式:a(b|be)c 表示‘a’,‘c’間需要存在 ‘b’,或者‘be’
- 圓括號:()表示組的概念 如上正則表達式:a(b|be)c ,括號內為一組
- 表示批次次數符號(符號前面單元出現次數)
- * 0次或者多次 例如正則表達式:"ab*" 表示‘a’后面的‘b’出現0次或者多次,再如表達式[0-9]* ,'' '1234','456'等都與之匹配
- + 1次或者多次 例如正則 表達式:“ab+” 表示‘a’后面‘b’出現1次或者多次, 再如表達式[0-9]*, 就不能是''
- 0次或者1次
- {n} n次 例如正則表達式:[0|1|2]{3}\-{1}[0-9]{8} 其中[0|1|2]{3} 表示0|1|2 出現三次, \-{1} 表示- 出現一次, [0-9]{8}表示0到9中數字出現8次。匹配: 021-64543215
- {n,m} n-m次
7. 否字符:^ 例如正則表達式:[^f][a-f][4] 表示由a-e之間字母組成的長(cháng)度為4的,不以f開(kāi)頭的字符串
8. 其他符號:
- \d [0-9]
\D [^0-9]
\w [A-Z0-9]
\W [^A-Z0-9]
\s [\t\n\r\f]
\S [^\t\n\r\f]
java API正則表達式API說(shuō)明:
Pattern類(lèi)介紹
static Pattern compile(String regex)將給定的正則表達式編譯并賦予給Pattern類(lèi)
- Pattern pattern = Pattern.compile("[0-9]{3,4}\\-{1}[0-9]{7,8}");
Pattern pattern = Pattern.compile("[0-9]{3,4}\\-{1}[0-9]{7,8}"); static Pattern compile(Stringregex, intflags)增加flag參數的指定,可選的flag參數包括:CASE_INSENSITIVE,MULTILINE,DOTALL,UNICODE_CASE,CANON_EQ
例如:
- Pattern pattern = Pattern.compile(".(abc)*",Pattern.CASE_INSENSITIVE);//匹配時(shí)候大小寫(xiě)不敏感
Pattern pattern = Pattern.compile(".(abc)*",Pattern.CASE_INSENSITIVE);//匹配時(shí)候大小寫(xiě)不敏感 pattern.flags() 返回當前Pattern的匹配flag參數.
Matcher matcher(CharSequence input) 生成一個(gè)給定命名的Matcher對象
例如:
- Matcher matcher = pattern.matcher(" ABC");
Matcher matcher = pattern.matcher(" ABC"); static boolean matches(String regex, CharSequence input) 返回匹配結果,不會(huì )返回對象
例如:
- boolean matchResult=Pattern.matches(".(abc)*", "ABC");
boolean matchResult=Pattern.matches(".(abc)*", "ABC"); String pattern()
返回該Patter對象所編譯的正則表達式。
String[] split(CharSequence input)
將目標字符串按照Pattern里所包含的正則表達式為模進(jìn)行分割。
String[] split(CharSequence input, int limit)
作用同上,增加參數limit目的在于要指定分割的段數,如將limi設為2,那么目標字符串將根據正則表達式分為割為兩段。
例如:
- Pattern pattern=Pattern.compile("[,]+");//用逗號分隔
- String[] result=pattern.split("Ok,good,Thank you");
- String[] result2=pattern.split("Ok,good Thank,you", 2);//2 分隔的段數
Pattern pattern=Pattern.compile("[,]+");//用逗號分隔String[] result=pattern.split("Ok,good,Thank you");String[] result2=pattern.split("Ok,good Thank,you", 2);//2 分隔的段數
Matcher類(lèi)介紹
看如下一段代碼:
- Pattern pattern = Pattern.compile("a.*string");
- Matcher matcher = pattern.matcher("a string");
-
- boolean didMatch = matcher.matches();
- System.out.println(didMatch);
-
- int patternStartIndex = matcher.start();
- System.out.println(patternStartIndex);
-
- int patternEndIndex = matcher.end();
- System.out.println(patternEndIndex);
Pattern pattern = Pattern.compile("a.*string");Matcher matcher = pattern.matcher("a string");boolean didMatch = matcher.matches();System.out.println(didMatch);int patternStartIndex = matcher.start();System.out.println(patternStartIndex);int patternEndIndex = matcher.end();System.out.println(patternEndIndex);
-
-
matches() 只告訴我們整個(gè)輸入順序是否與模式嚴格匹配。 -
start 告訴我們所匹配字符串起始的那個(gè)字符串中的索引值。 -
end() 告訴我們所匹配字符串終止的那個(gè)字符串中的索引值,并用該值減去 1。 - lookingAt ()方法將檢測目標字符串是否以匹配的子串起始。
- find()方法嘗試在目標字符串里查找下一個(gè)匹配子串。
- String group()返回當前查找而獲得的與組匹配的所有子串內容
- String group(int group) 返回當前查找而獲得的與指定的組匹配的子串內容
- int groupCount() 返回當前查找所獲得的匹配組的數量。
簡(jiǎn)單搜索運用,搜索出包含WikiWord主題的單詞
- package com.self.regexSample;
-
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- public class TestRegex {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
-
- String input = "Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.";
- Pattern pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+");
- Matcher matcher = pattern.matcher(input);
-
- while (matcher.find()) {
- System.out.println("Found this wiki word: " + matcher.group());
- }
-
-
-
- }
-
- }
package com.self.regexSample;import java.util.regex.Matcher;import java.util.regex.Pattern;public class TestRegex {/*** @param args*/public static void main(String[] args) {String input = "Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.";Pattern pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+");Matcher matcher = pattern.matcher(input);while (matcher.find()) {System.out.println("Found this wiki word: " + matcher.group());}}}
-
replaceAll() ,使用我們指定的一個(gè)字符串替換所有匹配字符串。 -
replaceFirst() ,只使用我們指定的一個(gè)字符串替換第一個(gè)匹配字符串。