//测试用的字符串
StringBuffer data = new StringBuffer();
data.append("<html><head></head><body>test java regex");
data.append("<img src=\"white.jpg\">");
data.append("hello world");
data.append("<IMG src=\"black.gif\"/>");
data.append("</body></html>");
测试示例1:
/**
* <pre>
* Pattern.CASE_INSENSITIVE : 忽略大小写
* . : 匹配任意一个字符
* * : 重复任意次
* ? : reluctant[懒惰]数量词,匹配最短的
* </pre>
* @param input
*/
public static void test1() {
Pattern p = Pattern.compile("<img.*?>", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
while (m.find()) {
System.out.println(m.group());
}
}
结果:
<img src="white.jpg">
<IMG src="black.gif"/>
<IMG src="black.gif"/>
测试示例2:
/**
* <pre>
* Pattern.CASE_INSENSITIVE : 忽略大小写
* . : 匹配任意一个字符
* * : 重复任意次
* </pre>
* @param input
*/
public static void test2() {
Pattern p = Pattern.compile("<img.*>", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
while (m.find()) {
System.out.println(m.group());
}
}
结果:
<img src="white.jpg">hello world<IMG src="black.gif"/></body></html>
测试示例3:
/**
* <pre>
* Matcher:appendReplacement(StringBuffer sb, String replacement)
* 将当前匹配子串替换为指定字符串,并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里
* Matcher:appendTail(StringBuffer sb)
* 将最后一次匹配工作后剩余的字符串添加到一个StringBuffer对象里。
* </pre>
* @param input
*/
public static void test3() {
Pattern p = Pattern.compile("<img.*?>", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb,"<img src=\"test.gif\">");
System.out.println(sb);
}
m.appendTail(sb);
System.out.println(sb);
}
结果:
<html><head></head><body>test java regex<img src="test.gif">
<html><head></head><body>test java regex<img src="test.gif">hello world<img src="test.gif">
<html><head></head><body>test java regex<img src="test.gif">hello world<img src="test.gif"></body></html>
<html><head></head><body>test java regex<img src="test.gif">hello world<img src="test.gif">
<html><head></head><body>test java regex<img src="test.gif">hello world<img src="test.gif"></body></html>
测试示例4:
/**
* Greedy 数量词
* <pre>
* x? : x一次或一次也没有
* </pre>
* @param input
*/
public static void test4() {
//测试用的字符串
StringBuffer data = new StringBuffer();
data.append("an apple,a banana");
Pattern p = Pattern.compile("(an)?", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
int count = 0;
while(m.find()){
System.out.println((++count)+":"+m.group());
}
}
结果:
1:an
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:an
13:an
14:
15:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:an
13:an
14:
15:
测试示例5:
/**
* Greedy 数量词
* <pre>
* x* : x零次或多次
* </pre>
* @param input
*/
public static void test5() {
//测试用的字符串
StringBuffer data = new StringBuffer();
data.append("an apple,a banana");
Pattern p = Pattern.compile("(an)*", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
int count = 0;
while(m.find()){
System.out.println((++count)+":"+m.group());
}
}
结果:
1:an
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:anan
13:
14:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:anan
13:
14:
测试示例6:
/**
* Greedy 数量词
* <pre>
* x+ : x一次或多次
* </pre>
* @param input
*/
public static void test6() {
//测试用的字符串
StringBuffer data = new StringBuffer();
data.append("an apple,a banana");
Pattern p = Pattern.compile("(an)+", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
int count = 0;
while(m.find()){
System.out.println((++count)+":"+m.group());
}
}
结果:
1:an
2:anan
2:anan
测试示例7:
/**
* Greedy 数量词
* <pre>
* x{n} : x,恰好n次
* </pre>
* @param input
*/
public static void test7() {
//测试用的字符串
StringBuffer data = new StringBuffer();
data.append("an apple,a banana");
Pattern p = Pattern.compile("(an){1}", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
int count = 0;
while(m.find()){
System.out.println((++count)+":"+m.group());
}
}
结果:
1:an
2:an
3:an
2:an
3:an
测试示例8:
/**
* Greedy 数量词
* <pre>
* x{n,m} : x,至少n次,但是不超过m次
* </pre>
* @param input
*/
public static void test8() {
//测试用的字符串
StringBuffer data = new StringBuffer();
data.append("an apple,a banana");
Pattern p = Pattern.compile("(an){1,2}", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
int count = 0;
while(m.find()){
System.out.println((++count)+":"+m.group());
}
}
结果:
1:an
2:anan
2:anan
/**
* Greedy 数量词
* <pre>
* x{n,} : x,至少n次
* </pre>
* @param input
*/
public static void test9() {
//测试用的字符串
StringBuffer data = new StringBuffer();
data.append("an apple,a banana");
Pattern p = Pattern.compile("(an){2,}", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(data);
int count = 0;
while(m.find()){
System.out.println((++count)+":"+m.group());
}
}
结果:
1:anan