public class RegexTest {public static void printMatched(String regex, String source) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(source);
StringBuffer sb=new StringBuffer();
while (m.find()) {
System.out.println(m.start());
System.out.println(m.end());
for (int i = 0; i <= m.groupCount(); i++) {
System.out.println(m.group(i));
sb.append(m.group(i));
}
}
//System.out.println(m.start());
//System.out.println(m.end());
System.out.println("--"+sb.toString());
}public static void main(String[] arg) {
RegexTest.printMatched("<(\\w+)>.*</(\\1)>",
"<table><td>sdjfjfiweif</td></table><cd>sdfsdf</cd>");}
}打印结果是:0
35
<table><td>sdjfjfiweif</td></table>
table
table
35
50
<cd>sdfsdf</cd>
cd
cd
--<table><td>sdjfjfiweif</td></table>tabletable<cd>sdfsdf</cd>cdcd对于打印的第 4行 第五行 第9行 第十行 结果不解 ,还有java里面的match.group是什么概念呀?
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(source);
StringBuffer sb=new StringBuffer();
while (m.find()) {
System.out.println(m.start());
System.out.println(m.end());
for (int i = 0; i <= m.groupCount(); i++) {
System.out.println(m.group(i));
sb.append(m.group(i));
}
}
//System.out.println(m.start());
//System.out.println(m.end());
System.out.println("--"+sb.toString());
}public static void main(String[] arg) {
RegexTest.printMatched("<(\\w+)>.*</(\\1)>",
"<table><td>sdjfjfiweif</td></table><cd>sdfsdf</cd>");}
}打印结果是:0
35
<table><td>sdjfjfiweif</td></table>
table
table
35
50
<cd>sdfsdf</cd>
cd
cd
--<table><td>sdjfjfiweif</td></table>tabletable<cd>sdfsdf</cd>cdcd对于打印的第 4行 第五行 第9行 第十行 结果不解 ,还有java里面的match.group是什么概念呀?
我认为最详实而不累赘的正则表达式教程:http://www.cnblogs.com/dragon/archive/2006/05/08/394078.html
复述:本次回答纯属打酱油,呵呵……