Skip to content

Commit d2a914a

Browse files
committed
merge
1 parent dcdf7b2 commit d2a914a

File tree

1 file changed

+119
-37
lines changed

1 file changed

+119
-37
lines changed

src/main/java/com/chen/api/util/regex/RegexTest3.java

Lines changed: 119 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import java.util.Arrays;
56
import java.util.regex.Matcher;
67
import java.util.regex.Pattern;
78

@@ -11,31 +12,27 @@
1112
*/
1213
public class RegexTest3 {
1314

14-
15-
/**
16-
* 演示pattern match对象
17-
*/
1815
@Test
1916
public void test1() {
2017

2118
Pattern pattern = Pattern.compile("\\d+");
2219

23-
Matcher matcher = pattern.matcher("56sd47f");
20+
Matcher matcher = pattern.matcher("56");
21+
22+
Boolean flag = Pattern.matches("\\d+", "test string 34 5");
23+
2424
//表示整个字符串是否可以与模式匹配
25-
//获取匹配的第一个分组,而且会消费字符串,后续的循环匹配不到56
26-
// boolean b = matcher.matches();
25+
boolean b = matcher.matches();
26+
// System.out.println("b=="+b);
27+
System.out.println(matcher.group());
2728

2829
while (matcher.find()) {
2930
System.out.println("result=" + matcher.group());
31+
System.out.println("count=" + matcher.groupCount());
3032
}
31-
32-
3333
}
3434

3535

36-
/**
37-
* 测试从指定位置查找字符串
38-
*/
3936
@Test
4037
public void test2() {
4138

@@ -56,70 +53,155 @@ public void test2() {
5653
}
5754

5855

59-
/**
60-
* 测试分组和分组的个数
61-
*/
6256
@Test
6357
public void test3() {
6458

6559
String poem =
66-
"Twas brillig, and the slithy toves\n" +
67-
"Did gyre and gimble in the wabe.\n" +
68-
"All mimsy were the borogoves,\n" +
69-
"And the mome raths outgrabe.\n\n" +
70-
"Beware the Jabberwock, my son,\n" +
71-
"The jaws that bite, the claws that catch.\n" +
72-
"Beware the Jubjub bird, and shun\n" +
60+
"Twas brillig, and the slithy toves/n" +
61+
"Did gyre and gimble in the wabe./n" +
62+
"All mimsy were the borogoves,/n" +
63+
"And the mome raths outgrabe./n/n" +
64+
"Beware the Jabberwock, my son,/n" +
65+
"The jaws that bite, the claws that catch./n" +
66+
"Beware the Jubjub bird, and shun/n" +
7367
"The frumious Bandersnatch.";
7468

7569
Pattern pattern = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))");
7670

7771
Matcher matcher = pattern.matcher(poem);
7872

7973
while (matcher.find()) {
80-
int count = matcher.groupCount();
81-
for (int i = 0; i <= count; i++) {
82-
String result = matcher.group(i);
83-
System.out.println(result);
74+
for (int i = 0; i <= matcher.groupCount(); i++) {
75+
System.out.println(matcher.group(i));
8476
}
8577
}
8678
}
8779

8880

89-
/**
90-
* 测试分组的开始位置和结束位置
91-
*/
9281
@Test
9382
public void test4() {
94-
String[] input = new String[]{"Java has regular expressions in 1.4", "regular expressions now expressing in Java", "Java represses oracular expressions"};
83+
String[] input = new String[]{
84+
"Java has regular expressions in 1.4",
85+
"regular expressions now expressing in Java",
86+
"Java represses oracular expressions"
87+
};
9588
Pattern p1 = Pattern.compile("re\\w*"),
9689
p2 = Pattern.compile("Java.*");
9790
for (int i = 0; i < input.length; i++) {
9891
System.out.println("input " + i + ": " + input[i]);
99-
Matcher m1 = p1.matcher(input[i]),
92+
Matcher
93+
m1 = p1.matcher(input[i]),
10094
m2 = p2.matcher(input[i]);
10195
while (m1.find()) {
102-
System.out.println("m1.find() '" + m1.group() + "' start = " + m1.start() + " end = " + m1.end());
96+
System.out.println("m1.find() '" + m1.group() +
97+
"' start = " + m1.start() + " end = " + m1.end());
10398
}
99+
104100
while (m2.find()) {
105-
System.out.println("m2.find() '" + m2.group() + "' start = " + m2.start() + " end = " + m2.end());
101+
System.out.println("m2.find() '" + m2.group() +
102+
"' start = " + m2.start() + " end = " + m2.end());
106103
}
107104
// No reset() necessary
108105
if (m1.lookingAt()) {
109-
System.out.println("m1.lookingAt() start = " + m1.start() + " end = " + m1.end());
106+
System.out.println("m1.lookingAt() start = "
107+
+ m1.start() + " end = " + m1.end());
110108
}
111109
if (m2.lookingAt()) {
112-
System.out.println("m2.lookingAt() start = " + m2.start() + " end = " + m2.end());
110+
System.out.println("m2.lookingAt() start = "
111+
+ m2.start() + " end = " + m2.end());
113112
}
114113
// No reset() necessary
115114
if (m1.matches()) {
116-
System.out.println("m1.matches() start = " + m1.start() + " end = " + m1.end());
115+
System.out.println("m1.matches() start = "
116+
+ m1.start() + " end = " + m1.end());
117117
}
118+
118119
if (m2.matches()) {
119-
System.out.println("m2.matches() start = " + m2.start() + " end = " + m2.end());
120+
System.out.println("m2.matches() start = "
121+
+ m2.start() + " end = " + m2.end());
120122
}
121123
}
122124
}
123125

124126

127+
@Test
128+
public void test5() {
129+
String input = "This!!unusual use!!of exclamation!!points";
130+
System.out.println(Arrays.asList(Pattern.compile("!!").split(input)));
131+
// Only do the first three:
132+
System.out.println(Arrays.asList(Pattern.compile("!!").split(input, 3)));
133+
System.out.println(Arrays.asList("Aha! String has a split() built in!".split(" ")));
134+
135+
}
136+
137+
138+
@Test
139+
public void test6() {
140+
141+
String s = "! Here's a block of text to use as input to\n" +
142+
" the regular expression matcher. Note that we'll\n" +
143+
" first extract the block of text by looking for\n" +
144+
" the special delimiters, then process the\n" +
145+
" extracted block. !";
146+
Matcher mInput = Pattern.compile("/*!(.*)!*/", Pattern.DOTALL).matcher(s);
147+
// Captured by parentheses
148+
if (mInput.find()){
149+
s = mInput.group(1);
150+
}
151+
152+
// Replace two or more spaces with a single space:
153+
s = s.replaceAll(" {2,}", " ");
154+
155+
// Replace one or more spaces at the beginning of each
156+
// line with no spaces. Must enable MULTILINE mode:
157+
s = s.replaceAll("(?m)^ +", "");
158+
System.out.println(s);
159+
s = s.replaceFirst("[aeiou]", "(VOWEL1)");
160+
StringBuffer sbuf = new StringBuffer();
161+
Pattern p = Pattern.compile("[aeiou]");
162+
Matcher m = p.matcher(s);
163+
// Process the find information as you
164+
// perform the replacements:
165+
while (m.find())
166+
m.appendReplacement(sbuf, m.group().toUpperCase());
167+
// Put in the remainder of the text:
168+
m.appendTail(sbuf);
169+
System.out.println(sbuf);
170+
171+
}
172+
173+
174+
@Test
175+
public void test7() {
176+
Matcher m = Pattern.compile("[frb][aiu][gx]")
177+
.matcher("fix the rug with bags");
178+
179+
while (m.find()) {
180+
System.out.println(m.group());
181+
}
182+
183+
m.reset("fix the rig with rags");
184+
185+
while (m.find()) {
186+
System.out.println(m.group());
187+
}
188+
}
189+
190+
191+
@Test
192+
public void test8(){
193+
194+
195+
196+
}
197+
198+
199+
200+
201+
202+
203+
204+
205+
206+
125207
}

0 commit comments

Comments
 (0)