Skip to content

Commit 3b23458

Browse files
author
chenweijie
committed
提交正则表达式相关的代码
1 parent 331035d commit 3b23458

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,36 @@ public void testNumber() {
5555
Arrays.asList(strings).forEach(str -> System.out.println(str + ",matches:" + str.matches(patterStr)));
5656
}
5757

58+
/**
59+
* 测试换行模式
60+
*/
61+
@Test
62+
public void testPattern() {
63+
Matcher m = Pattern.compile("(?ms)^.*$").matcher("a\r\nb\r\nc");
64+
while (m.find()) {
65+
System.out.println("matched:" + m.group());
66+
}
67+
}
68+
69+
70+
/**
71+
* 测试回溯
72+
*/
73+
@Test
74+
public void testMatch() {
75+
String str = "12345";
76+
77+
Pattern pattern = Pattern.compile("(\\d{1,3})(\\d{1,3})");
78+
79+
Matcher matcher = pattern.matcher(str);
80+
81+
while (matcher.find()) {
82+
System.out.println("count:" + matcher.groupCount());
83+
for (int i = 0; i < matcher.groupCount(); i++) {
84+
System.out.println(matcher.group(i));
85+
}
86+
}
87+
}
88+
5889

5990
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,31 @@
1111
*/
1212
public class RegexTest3 {
1313

14+
15+
/**
16+
* 演示pattern match对象
17+
*/
1418
@Test
1519
public void test1() {
1620

1721
Pattern pattern = Pattern.compile("\\d+");
1822

19-
Matcher matcher = pattern.matcher("56");
23+
Matcher matcher = pattern.matcher("56sd47f");
2024
//表示整个字符串是否可以与模式匹配
21-
boolean b = matcher.matches();
22-
matcher.group();
23-
// System.out.println("b=="+b);
24-
System.out.println(matcher.group());
25+
//获取匹配的第一个分组,而且会消费字符串,后续的循环匹配不到56
26+
// boolean b = matcher.matches();
2527

2628
while (matcher.find()) {
2729
System.out.println("result=" + matcher.group());
28-
System.out.println("count=" + matcher.groupCount());
2930
}
31+
32+
3033
}
3134

3235

36+
/**
37+
* 测试从指定位置查找字符串
38+
*/
3339
@Test
3440
public void test2() {
3541

@@ -50,6 +56,9 @@ public void test2() {
5056
}
5157

5258

59+
/**
60+
* 测试分组和分组的个数
61+
*/
5362
@Test
5463
public void test3() {
5564

@@ -77,6 +86,9 @@ public void test3() {
7786
}
7887

7988

89+
/**
90+
* 测试分组的开始位置和结束位置
91+
*/
8092
@Test
8193
public void test4() {
8294
String[] input = new String[]{"Java has regular expressions in 1.4", "regular expressions now expressing in Java", "Java represses oracular expressions"};

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import org.junit.Test;
44

5-
import java.util.Arrays;
6-
import java.util.List;
75
import java.util.regex.Matcher;
86
import java.util.regex.Pattern;
97

@@ -16,23 +14,28 @@
1614
public class TestRegularExpression {
1715

1816

19-
17+
/**
18+
* 测试split方法
19+
*/
2020
@Test
21-
public void test1(){
21+
public void test1() {
2222

2323
String input = "This !! unusual use!!of exclamation!!points";
2424

25-
Pattern pattern =Pattern.compile("!!");
26-
String [] arr = pattern.split(input);
25+
Pattern pattern = Pattern.compile("!!");
26+
String[] arr = pattern.split(input);
2727
StringBuffer sb = new StringBuffer();
28-
pattern.matcher(input).appendReplacement(sb,arr[0].toUpperCase()).appendTail(sb);
28+
pattern.matcher(input).appendReplacement(sb, arr[0].toUpperCase()).appendTail(sb);
2929

30-
String stringSpit ="this is a String!";
30+
String stringSpit = "this is a String!";
3131
String[] arr2 = stringSpit.split("\\n");
3232

3333
}
3434

3535

36+
/**
37+
* 测试Matcher.reset()方法
38+
*/
3639
@Test
3740
public void test2() {
3841

@@ -47,13 +50,7 @@ public void test2() {
4750
while (m.find()) {
4851
System.out.println(m.group());
4952
}
50-
51-
5253
}
5354

5455

55-
56-
57-
58-
5956
}

src/main/test/com/chen/test/TestCase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public void test() {
2222
final int TIDYING = 2 << COUNT_BITS;
2323
final int TERMINATED = 3 << COUNT_BITS;
2424

25-
2625
}
2726

2827

0 commit comments

Comments
 (0)