寫了個Core Java源程序中的行號去掉的程序。
Java中使用正則表達式,一般過程是
Pattern pattern = Pattern.compile("\\d+\\. (\\s*.*)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {}
但這樣并沒有用到分組,小括號里的內容并不會單獨列出。
如果要去掉行號,只想要小括號里的部分,使用group方法
System.out.println(matcher.group(1));
group(0)和group()效果一樣,返回整行內容
group(x)則是返回和第x組小括號匹配的內容(x<=1)