1
2
3
4
5
6
7
8
9
10
11
12
13
|
BufferedReader
in
;
Pattern pattern = Pattern.compile(
"//(//d{3}//)//s//d{3}-//d{4}"
);
in
=
new
BufferedReader(
new
FileReader(
"phone"
));
String
s;
while
((s =
in
.readLine()) !=
null
)
{
Matcher matcher = pattern.matcher(s);
if
(matcher.find())
{
System.out.println(matcher.group());
}
}
in
.close();
|
1
2
3
4
5
6
|
interface
CharSequence {
charAt(
int
i);
length();
subSequence(
int
start,
int
end);
toString();
}
|
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
|
//: c12:TestRegularExpression.java
// Allows you to easly try out regular expressions.
// {Args: abcabcabcdefabc "abc+" "(abc)+" "(abc){2,}" }
import
java.util.regex.*;
publicclass TestRegularExpression {
publicstaticvoid main(
String
[] args) {
if
(args.length <
2
) {
System.out.println(
"Usage:/n"
+
"java TestRegularExpression "
+
"characterSequence regularExpression+"
);
System.exit(
0
);
}
System.out.println(
"Input: /"
" + args[0] + "
/
""
);
for
(
int
i =
1
; i < args.length; i++) {
System.out.println(
"Regular expression: /"
" + args[i] + "
/
""
);
Pattern p = Pattern.compile(args[i]);
Matcher m = p.matcher(args[
0
]);
while
(m.find()) {
System.out.println(
"Match /"
" + m.group() +
"/"
at positions " +
m.start() +
"-"
+ (m.end() -
1
));
}
}
}
}
///:~
|
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
|
//: c12:FindDemo.java
import
java.util.regex.*;
import
com.bruceeckel.simpletest.*;
import
java.util.*;
publicclass FindDemo {
privatestatic Test monitor =
new
Test();
publicstaticvoid main(
String
[] args) {
Matcher m = Pattern.compile(
"//w+"
)
.matcher(
"Evening is full of the linnet's wings"
);
while
(m.find())
System.out.println(m.group());
int
i =
0
;
while
(m.find(i)) {
System.out.print(m.group() +
" "
);
i++;
}
monitor.expect(
new
String
[] {
"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 "
});
}
}
///:~
|
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
|
//: c12:Groups.java
import
java.util.regex.*;
import
com.bruceeckel.simpletest.*;
publicclass Groups {
privatestatic Test monitor =
new
Test();
staticpublicfinal
String
poem =
"Twas brillig, and the slithy toves/n"
+
"Did gyre and gimble in the wabe./n"
+
"All mimsy were the borogoves,/n"
+
"And the mome raths outgrabe./n/n"
+
"Beware the Jabberwock, my son,/n"
+
"The jaws that bite, the claws that catch./n"
+
"Beware the Jubjub bird, and shun/n"
+
"The frumious Bandersnatch."
;
publicstaticvoid main(
String
[] args) {
Matcher m =
Pattern.compile("(?m)(
//S+)//s+((//S+)//s+(//S+))___FCKpd___6quot;)
.matcher(poem);
while
(m.find()) {
for
(
int
j =
0
; j <= m.groupCount(); j++)
System.out.print(
"["
+ m.group(j) +
"]"
);
System.out.println();
}
monitor.expect(
new
String
[]{
"[the slithy toves]"
+
"[the][slithy toves][slithy][toves]"
,
"[in the wabe.][in][the wabe.][the][wabe.]"
,
"[were the borogoves,]"
+
"[were][the borogoves,][the][borogoves,]"
,
"[mome raths outgrabe.]"
+
"[mome][raths outgrabe.][raths][outgrabe.]"
,
"[Jabberwock, my son,]"
+
"[Jabberwock,][my son,][my][son,]"
,
"[claws that catch.]"
+
"[claws][that catch.][that][catch.]"
,
"[bird, and shun][bird,][and shun][and][shun]"
,
"[The frumious Bandersnatch.][The]"
+
"[frumious Bandersnatch.][frumious][Bandersnatch.]"
});
}
}
///:~
|
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
|
//: c12:StartEnd.java
import
java.util.regex.*;
import
com.bruceeckel.simpletest.*;
publicclass StartEnd {
privatestatic Test monitor =
new
Test();
publicstaticvoid main(
String
[] args) {
String
[] input =
new
String
[] {
"Java has regular expressions in 1.4"
,
"regular expressions now expressing in Java"
,
"Java represses oracular expressions"
};
Pattern
p1 = Pattern.compile(
"re//w*"
),
p2 = Pattern.compile(
"Java.*"
);
for
(
int
i =
0
; i < input.length; i++) {
System.out.println(
"input "
+ i +
": "
+ input[i]);
Matcher
m1 = p1.matcher(input[i]),
m2 = p2.matcher(input[i]);
while
(m1.find())
System.out.println(
"m1.find() '"
+ m1.group() +
"' start = "
+ m1.start() +
" end = "
+ m1.end());
while
(m2.find())
System.out.println(
"m2.find() '"
+ m2.group() +
"' start = "
+ m2.start() +
" end = "
+ m2.end());
if
(m1.lookingAt())
// No reset() necessary
System.out.println(
"m1.lookingAt() start = "
+ m1.start() +
" end = "
+ m1.end());
if
(m2.lookingAt())
System.out.println(
"m2.lookingAt() start = "
+ m2.start() +
" end = "
+ m2.end());
if
(m1.matches())
// No reset() necessary
System.out.println(
"m1.matches() start = "
+ m1.start() +
" end = "
+ m1.end());
if
(m2.matches())
System.out.println(
"m2.matches() start = "
+ m2.start() +
" end = "
+ m2.end());
}
monitor.expect(
new
String
[] {
"input 0: Java has regular expressions in 1.4"
,
"m1.find() 'regular' start = 9 end = 16"
,
"m1.find() 'ressions' start = 20 end = 28"
,
"m2.find() 'Java has regular expressions in 1.4'"
+
" start = 0 end = 35"
,
"m2.lookingAt() start = 0 end = 35"
,
"m2.matches() start = 0 end = 35"
,
"input 1: regular expressions now "
+
"expressing in Java"
,
"m1.find() 'regular' start = 0 end = 7"
,
"m1.find() 'ressions' start = 11 end = 19"
,
"m1.find() 'ressing' start = 27 end = 34"
,
"m2.find() 'Java' start = 38 end = 42"
,
"m1.lookingAt() start = 0 end = 7"
,
"input 2: Java represses oracular expressions"
,
"m1.find() 'represses' start = 5 end = 14"
,
"m1.find() 'ressions' start = 27 end = 35"
,
"m2.find() 'Java represses oracular expressions' "
+
"start = 0 end = 35"
,
"m2.lookingAt() start = 0 end = 35"
,
"m2.matches() start = 0 end = 35"
});
}
}
///:~
|
编译标志 | 效果 |
Pattern.CANON_EQ | 当且仅当两个字符的"正规分解(canonical decomposition)"都完全相同的情况下,才认定匹配。比如用了这个标志之后,表达式"a/u030A"会匹配"?"。默认情况下,不考虑"规范相等性(canonical equivalence)"。 |
默认情况下,大小写不明感的匹配只适用于US-ASCII字符集。这个标志能让表达式忽略大小写进行匹配。要想对Unicode字符进行大小不明感的匹配,只要将UNICODE_CASE与这个标志合起来就行了。 | |
在这种模式下,匹配时会忽略(正则表达式里的)空格字符(注:不是指表达式里的"//s",而是指表达式里的空格,tab,回车之类)。注释从#开始,一直到这行结束。可以通过嵌入式的标志来启用Unix行模式。 | |
在这种模式下,表达式'.'可以匹配任意字符,包括表示一行的结束符。默认情况下,表达式'.'不匹配行的结束符。 | |
在这种模式下,'^'和'$'分别匹配一行的开始和结束。此外,'^'仍然匹配字符串的开始,'$'也匹配字符串的结束。默认情况下,这两个表达式仅仅匹配字符串的开始和结束。 | |
在这个模式下,如果你还启用了CASE_INSENSITIVE标志,那么它会对Unicode字符进行大小写不明感的匹配。默认情况下,大小写不明感的匹配只适用于US-ASCII字符集。 | |
在这个模式下,只有'/n'才被认作一行的中止,并且与'.','^',以及'$'进行匹配。 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import
java.util.regex.*;
import
com.bruceeckel.simpletest.*;
publicclass ReFlags {
privatestatic Test monitor =
new
Test();
publicstaticvoid main(
String
[] args) {
Pattern p = Pattern.compile(
"^java"
,
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher m = p.matcher(
"java has regex/nJava has regex/n"
+
"JAVA has pretty good regular expressions/n"
+
"Regular expressions are in Java"
);
while
(m.find())
System.out.println(m.group());
monitor.expect(
new
String
[] {
"java"
,
"Java"
,
"JAVA"
});
}
}
///:~
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import
java.util.regex.*;
import
com.bruceeckel.simpletest.*;
import
java.util.*;
publicclass SplitDemo {
privatestatic Test monitor =
new
Test();
publicstaticvoid main(
String
[] args) {
String
input =
"This!!unusual use!!of exclamation!!points"
;
System.out.println(Arrays.asList(
Pattern.compile(
"!!"
).split(input)));
// Only do the first three:
System.out.println(Arrays.asList(
Pattern.compile(
"!!"
).split(input,
3
)));
System.out.println(Arrays.asList(
"Aha! String has a split() built in!"
.split(
" "
)));
monitor.expect(
new
String
[] {
"[This, unusual use, of exclamation, points]"
,
"[This, unusual use, of exclamation!!points]"
,
"[Aha!, String, has, a, split(), built, in!]"
});
}
}
///:~
|
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
|
//: c12:TheReplacements.java
import
java.util.regex.*;
import
java.io.*;
import
com.bruceeckel.util.*;
import
com.bruceeckel.simpletest.*;
/*! Here's a block of text to use as input to
the regular expression matcher. Note that we'll
first extract the block of text by looking for
the special delimiters, then process the
extracted block. !*/
publicclass TheReplacements {
privatestatic Test monitor =
new
Test();
publicstaticvoid main(
String
[] args) throws Exception {
String
s = TextFile.read(
"TheReplacements.java"
);
// Match the specially-commented block of text above:
Matcher mInput =
Pattern.compile(
"///*!(.*)!//*/"
, Pattern.DOTALL)
.matcher(s);
if
(mInput.find())
s = mInput.group(
1
);
// Captured by parentheses
// Replace two or more spaces with a single space:
s = s.replaceAll(
" {2,}"
,
" "
);
// Replace one or more spaces at the beginning of each
// line with no spaces. Must enable MULTILINE mode:
s = s.replaceAll(
"(?m)^ +"
,
""
);
System.out.println(s);
s = s.replaceFirst(
"[aeiou]"
,
"(VOWEL1)"
);
StringBuffer sbuf =
new
StringBuffer();
Pattern p = Pattern.compile(
"[aeiou]"
);
Matcher m = p.matcher(s);
// Process the find information as you
// perform the replacements:
while
(m.find())
m.appendReplacement(sbuf, m.group().toUpperCase());
// Put in the remainder of the text:
m.appendTail(sbuf);
System.out.println(sbuf);
monitor.expect(
new
String
[]{
"Here's a block of text to use as input to"
,
"the regular expression matcher. Note that we'll"
,
"first extract the block of text by looking for"
,
"the special delimiters, then process the"
,
"extracted block. "
,
"H(VOWEL1)rE's A blOck Of tExt tO UsE As InpUt tO"
,
"thE rEgUlAr ExprEssIOn mAtchEr. NOtE thAt wE'll"
,
"fIrst ExtrAct thE blOck Of tExt by lOOkIng fOr"
,
"thE spEcIAl dElImItErs, thEn prOcEss thE"
,
"ExtrActEd blOck. "
});
}
}
///:~
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//: c12:Resetting.java
import
java.util.regex.*;
import
java.io.*;
import
com.bruceeckel.simpletest.*;
publicclass Resetting {
privatestatic Test monitor =
new
Test();
publicstaticvoid main(
String
[] args) throws Exception {
Matcher m = Pattern.compile(
"[frb][aiu][gx]"
)
.matcher(
"fix the rug with bags"
);
while
(m.find())
System.out.println(m.group());
m.reset(
"fix the rig with rags"
);
while
(m.find())
System.out.println(m.group());
monitor.expect(
new
String
[]{
"fix"
,
"rug"
,
"bag"
,
"fix"
,
"rig"
,
"rag"
});
}
}
///:~
|