forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinding.java
More file actions
70 lines (55 loc) · 1.94 KB
/
Finding.java
File metadata and controls
70 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// strings/Finding.java
// (c)2020 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.regex.*;
public class Finding {
public static void main(String[] args) {
Matcher m = Pattern.compile("\\w+")
.matcher(
"Evening is full of the linnet's wings");
while(m.find())
System.err.print(m.group() + " ");
System.err.println();
int i = 0;
while(m.find(i)) {
System.err.print(m.group() + " ");
i++;
}
test();
}
public static void test() {
Pattern pattern = Pattern.compile("\\d{3,5}");
String charSequence = "123-34345-234-00";
Matcher matcher = pattern.matcher(charSequence);
//虽然匹配失败,但由于charSequence里面的"123"和pattern是匹配的,所以下次的匹配从位置4开始
// print(matcher.matches());
//测试匹配位置
matcher.find();
matcher.find();
print("orginal:"+matcher.start());
//使用reset方法重置匹配位置
matcher.reset();
//第一次find匹配以及匹配的目标和匹配的起始位置
print(matcher.find());
print(matcher.group()+" - "+matcher.start());
//第二次find匹配以及匹配的目标和匹配的起始位置
print(matcher.find());
print(matcher.group()+" - "+matcher.start());
//第一次lookingAt匹配以及匹配的目标和匹配的起始位置
print(matcher.lookingAt());
print(matcher.group()+" - "+matcher.start());
//第二次lookingAt匹配以及匹配的目标和匹配的起始位置
print(matcher.lookingAt());
print(matcher.group()+" - "+matcher.start());
}
public static void print(Object o){
System.out.println(o);
}
}
/* Output:
Evening is full of the linnet s wings
Evening vening ening ning ing ng g is is s full full
ull ll l of of f the the he e linnet linnet innet nnet
net et t s s wings wings ings ngs gs s
*/