2013年11月17日星期日

java regex problem

Regular expression "\ b" indicates a word boundary , but I do not know how to test, I can not write the code has been measured , experts who help guide guidance.
Pattern pattern = Pattern.compile ("\ bdog \ b");
Matcher matcher = pattern.matcher ("The dog plays dog in the yard.");
while (matcher.find ()) {
System.out.println (matcher.group ());
}
output is empty.
If you use "\ \ bdog \ \ b" , then you can , but the java "\ b" should not be , and "\ t" kind of the same? Is to use a backslash should also be , and why ?
------ Solution ---------------------------------------- ----
\ b word boundary but \ is the escape character at the same time , to complete the presentation of \ b, we should let \ b in front of \ lose its special meaning , becoming common \ , use \ \ b , the first \ represents the escape character \ , the second \ is \ b in front of \ .
------ For reference only ------------------------------------ ---
\ is the escape symbol
------ For reference only --------------------------- ------------
not pay attention to the situation here need to be escaped .
------ For reference only -------------------------------------- -
Pattern pattern = Pattern.compile("(dog)");
Matcher matcher = pattern.matcher("a dog b dog c dog d");
while (matcher.find()) {
System.out.println(matcher.group());
}

没有评论:

发表评论