最近在正則的使用中炎咖,遇到了一些問題,問題比較多的躁锡,就是分組捕獲,
特別是貪婪這塊狮辽,在這里記錄一下;
參考資料:
http://tutorials.jenkov.com/java-regex/matcher.html#lookingat-method
http://stackoverflow.com/questions/16771177/what-does-the-regex-mean
http://www.regular-expressions.info/repeat.html
http://alan-hjkl.iteye.com/blog/1543548
http://blog.csdn.net/yin380697242/article/details/52097679
-1.切記:
在正則表達(dá)式中 '()' 這個(gè)才是表示一個(gè)分組;
- 正則表達(dá)式中以’()’標(biāo)記的子表達(dá)式所匹配的內(nèi)容就是一個(gè)分組(group).
- 類似于(?:pattern)格式的子表達(dá)式不能算是一個(gè)分組
- 分組索引是從1開始的,0代表正則表達(dá)式匹配的整個(gè)字符串,group(i)代表第i組匹配的內(nèi)容
- groupCount() 函數(shù)返回當(dāng)前正則表達(dá)式中分組的個(gè)數(shù)
0.貪婪、非貪婪、侵占
貪婪 (, ?, +):*
最大長度的匹配必孤,就是貪婪,默認(rèn)是貪婪匹配较屿,方式是從后往前匹配隧魄;
非貪婪 (?, ??, +?)*
匹配到結(jié)果就好,較少字符的匹配隘蝎,方式是從前往后匹配;
侵占 (+, ?+, ++)*
讀入整個(gè)串襟企,從前往后匹配嘱么,匹配的是整個(gè)串;
// 貪婪
String str ="aaa\"bbb\"ccc\"ddd\"eee";
Utils.println(str);
Utils.println(str.replaceAll("\"(.*)\"", "@"));
// 非貪婪
Utils.println(str.replaceAll("\"(.*?)\"", "@"));
輸出
Paste_Image.png
1.單個(gè)分組使用
獲取字符串中的 連續(xù)5位的 數(shù)字字符串
String sss = "131 46429 640 151 67801 234";
Pattern ppp = Pattern.compile("(\\d{5})");
Matcher mmm = ppp.matcher(sss);
while(mmm.find()) {
out("found: " + mmm.group()); // 單個(gè)分組 結(jié)果與 group(1)一致
}
Paste_Image.png
2.多個(gè)分組
/** '()' 分組 group **/
Pattern tPattern = Pattern.compile("(\\d{3,5})([a-z]{2})");
String tStr4 = "123bd-3434dc-34333dd-00";
Matcher tMatcher = tPattern.matcher(tStr4);
while (tMatcher.find()) {
// out(tMatcher.group(0)); // 等于字符串本身
out(tMatcher.group()); // 全組 (\d{3,5})([a-z]{2}) 輸出: 123bd 3434dc 34333dd
out(tMatcher.group(1)); // 第一組 (d{3,5}) 輸出: 123 3434 34333
out(tMatcher.group(2)); // 第二組 ([a-z]{2}) 輸出: bd dc dd
}
Paste_Image.png
3.嵌套分組
來自官方的說明(從左到右):
Paste_Image.png
String text = "John writes about this, and John Doe writes about that, and John Wayne writes about everything.";
String patternString1 = "((John) (.+?)) "; // 非貪婪模式
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
// group1: "((John) (.+?)) " Json 字符串 加上下一個(gè)字符串直到空格結(jié)尾
// group2: (John)
System.out.println("found: <" + matcher.group(1) +"> <" + matcher.group(2) + "> <" + matcher.group(3) + ">");
}
** 說明:**
- group(0): 字符串本身 也就是:“ John .+? ”
- 第一組:“((John) (.+?)) ” => Json 字符串 加上下一個(gè)字符串直到空格結(jié)尾
- 第二組:(John)
- 第三組:(.+?)
輸出:
Paste_Image.png
正則改成:
String patternString1 = "((John)(.+?))"; // 非貪婪模式
輸出
Paste_Image.png
正則改成:
String patternString1 = "((John)(.+?))"; // 非貪婪模式
輸出
Paste_Image.png
4.分組別名
在取分組的時(shí)候顽悼,我們需要傳遞 1,2這樣組號(hào)曼振,當(dāng)然也可以指定別名,
別名語法:(?<名稱>)
如下:
public static void test2() {
final String input = "范德薩#視頻# @李卓 查看圖片 哈哈哈 #java# java 是一名藝術(shù)" +
"#kotlin# kotlin 語言 查看圖片 @胡峰 @李饒 范德薩 查看圖片 查看圖片"; // #視頻# @李卓 查看圖片
// "(@.+?\\s)|(#.+?#)|(查看圖片)+?";
final String regex = "(?<g1>@.+?\\s)|(?<g2>#.+?#)|(?<g3>查看圖片)+?";
final Pattern p = Pattern.compile(regex);
final Matcher matcher = p.matcher(input);
while (matcher.find()) {
String result = matcher.group("g1");
if(result == null)
result = matcher.group("g2");
if(result == null)
result = matcher.group("g3");
System.out.println(result);
}
}