Skip to content

Commit f30d3df

Browse files
committed
正则表达式2
1 parent 1d1affe commit f30d3df

File tree

2 files changed

+78
-23
lines changed

2 files changed

+78
-23
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.chen.api.util.regex;
2+
3+
import org.junit.Test;
4+
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
/**
9+
* @author zhuningning
10+
* date 2017-11-18
11+
*/
12+
public class RegexTest3 {
13+
14+
@Test
15+
public void test1() {
16+
17+
Pattern pattern = Pattern.compile("\\d+");
18+
19+
Matcher matcher = pattern.matcher("56");
20+
//表示整个字符串是否可以与模式匹配
21+
boolean b = matcher.matches();
22+
matcher.group();
23+
// System.out.println("b=="+b);
24+
System.out.println(matcher.group());
25+
26+
while (matcher.find()) {
27+
System.out.println("result=" + matcher.group());
28+
System.out.println("count=" + matcher.groupCount());
29+
}
30+
}
31+
32+
33+
@Test
34+
public void test2() {
35+
36+
Pattern pattern = Pattern.compile("\\w+");
37+
38+
Matcher matcher = pattern.matcher("Evening is full of the linnet's wings");
39+
40+
while (matcher.find()) {
41+
System.out.println("match find:" + matcher.group());
42+
}
43+
44+
int i = 0;
45+
while (matcher.find(i)) {
46+
System.out.println(matcher.group());
47+
i++;
48+
}
49+
50+
}
51+
52+
53+
@Test
54+
public void test3() {
55+
56+
String poem =
57+
"Twas brillig, and the slithy toves/n" +
58+
"Did gyre and gimble in the wabe./n" +
59+
"All mimsy were the borogoves,/n" +
60+
"And the mome raths outgrabe./n/n" +
61+
"Beware the Jabberwock, my son,/n" +
62+
"The jaws that bite, the claws that catch./n" +
63+
"Beware the Jubjub bird, and shun/n" +
64+
"The frumious Bandersnatch.";
65+
66+
Pattern pattern = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))");
67+
68+
Matcher matcher = pattern.matcher(poem);
69+
70+
while (matcher.find()) {
71+
for (int i = 0; i <= matcher.groupCount(); i++) {
72+
System.out.println(matcher.group(i));
73+
}
74+
}
75+
}
76+
77+
78+
}

src/main/java/com/chen/regex/TestImg.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)