正則表達式的捕獲組(Java)

 捕獲組分類

  1. 普通捕獲組(Expression)
  2. 命名捕獲組(?<name>Expression)

普通捕獲組

從正則表達式左側開始,每出現一個左括號「(」記作一個分組,分組編號從1開始。0表明整個表達式。css

對於時間字符串:2017-04-25,表達式以下java

(\\d{4})-((\\d{2})-(\\d{2}))

有4個左括號,因此有4個分組正則表達式

public static final String DATE_STRING = "2017-04-25";
public static final String P_COMM = "(\\d{4})-((\\d{2})-(\\d{2}))";

Pattern pattern = Pattern.compile(P_COMM);
Matcher matcher = pattern.matcher(DATE_STRING);
matcher.find();//必需要有這句
System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0));
System.out.printf("\nmatcher.group(1) value:%s", matcher.group(1));
System.out.printf("\nmatcher.group(2) value:%s", matcher.group(2));
System.out.printf("\nmatcher.group(3) value:%s", matcher.group(3));
System.out.printf("\nmatcher.group(4) value:%s", matcher.group(4));

命名捕獲組

每一個以左括號開始的捕獲組,都緊跟着「?」,然後纔是正則表達式。spa

對於時間字符串:2017-04-25,表達式以下code



有4個命名的捕獲組,分別是(?<year>\\d{4})-(?<md>(?<month>\\d{2})-(?<date>\\d{2}))

命名的捕獲組一樣也能夠使用編號獲取相應值xml

public static final String P_NAMED = "(?<year>\\d{4})-(?<md>(?<month>\\d{2})-(?<date>\\d{2}))";
public static final String DATE_STRING = "2017-04-25";

Pattern pattern = Pattern.compile(P_NAMED);
Matcher matcher = pattern.matcher(DATE_STRING);
matcher.find();
System.out.printf("\n===========使用名稱獲取=============");
System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0));
System.out.printf("\n matcher.group('year') value:%s", matcher.group("year"));
System.out.printf("\nmatcher.group('md') value:%s", matcher.group("md"));
System.out.printf("\nmatcher.group('month') value:%s", matcher.group("month"));
System.out.printf("\nmatcher.group('date') value:%s", matcher.group("date"));
matcher.reset();
System.out.printf("\n===========使用編號獲取=============");
matcher.find();
System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0));
System.out.printf("\nmatcher.group(1) value:%s", matcher.group(1));
System.out.printf("\nmatcher.group(2) value:%s", matcher.group(2));
System.out.printf("\nmatcher.group(3) value:%s", matcher.group(3));
System.out.printf("\nmatcher.group(4) value:%s", matcher.group(4));

PS:非捕獲組
在左括號後緊跟「?:」,然後再加上正則表達式,構成非捕獲組(?:Expression)。blog

對於時間字符串:2017-04-25,表達式以下索引

(?:\\d{4})-((\\d{2})-(\\d{2}))
1
這個正則表達式雖然有四個左括號,理論上有4個捕獲組。可是第一組(?:\d{4}),實際上是被忽略的。當使用matcher.group(4)時,系統會報錯。字符串

public static final String P_UNCAP = "(?:\\d{4})-((\\d{2})-(\\d{2}))";
public static final String DATE_STRING = "2017-04-25";

Pattern pattern = Pattern.compile(P_UNCAP);
Matcher matcher = pattern.matcher(DATE_STRING);
matcher.find();
System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0));
System.out.printf("\nmatcher.group(1) value:%s", matcher.group(1));
System.out.printf("\nmatcher.group(2) value:%s", matcher.group(2));
System.out.printf("\nmatcher.group(3) value:%s", matcher.group(3));

// Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 4
System.out.printf("\nmatcher.group(4) value:%s", matcher.group(4));

 

//若是同時匹配到了不一樣位置的字符串,要取捕獲組哪一個位置的字符串
//這個位置在循環中索引值不能變,如m.group(2)中的2就不能變
String s=" from aaa from bbb"
Pattern p = Pattern.compile("\\s+(from|join)\\s+(\\w+)");
Matcher m = p.matcher(hql);
while (m.find()) {
   System.out.println(m.group(2));
}
//輸出結果:
aaa
bbb
相關文章
相關標籤/搜索