import java.util.regex.*;
import static net.mindview.util.Print.*;public class P303EX12 {
static public final 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."; public static void main(String[] args) {
int count = 0;
Matcher m = Pattern.compile("(^[^A-Z]|\\s+[^A-Z])\\w+").matcher(POEM);
// Matcher m = Pattern.compile("(^[a-z]|\\s+[a-z])\\w+").matcher(POEM);
while (m.find()) {
for (int j = 0; j <= m.groupCount(); j++) {
printnb(m.group(j));
}
count++;
}
print();
print(count);
}
}
代码如上,Thinking in Java第四版第13章第12题。使用第一个m的时候,会多出一个Beware,这个结果应该是不匹配的,求助为什么\n\n就会出错,\s+应该是多个空白字符才对啊
import static net.mindview.util.Print.*;public class P303EX12 {
static public final 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."; public static void main(String[] args) {
int count = 0;
Matcher m = Pattern.compile("(^[^A-Z]|\\s+[^A-Z])\\w+").matcher(POEM);
// Matcher m = Pattern.compile("(^[a-z]|\\s+[a-z])\\w+").matcher(POEM);
while (m.find()) {
for (int j = 0; j <= m.groupCount(); j++) {
printnb(m.group(j));
}
count++;
}
print();
print(count);
}
}
代码如上,Thinking in Java第四版第13章第12题。使用第一个m的时候,会多出一个Beware,这个结果应该是不匹配的,求助为什么\n\n就会出错,\s+应该是多个空白字符才对啊
printnb(m.group(j)+"##");