package twentyfive;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class five2 {
public static void main(String[] args) {
getDemo2();
}
public static void getDemo2()
{
//String str="i love china chian success forever four five"+System.getProperty("line.separator");
String str="i love china chian success forever four five";
System.out.print("\n");
System.out.print(str);
String reg="\\b[a-z]{4}\\b";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(str);
while(m.find())
{
System.out.println(m.group());
System.out.println(m.start()+"....."+m.end());
}
}
}
这段程序输出结果为:
i love china chian success forever four fivelove
2.....6
four
37.....41
five
42.....46
应该love这个字符在2.....6上面的而不是字符串结尾,而System.getProperty("line.separator")这个方法解决了这个问题输出为:
i love china chian success forever four five
love
2.....6
four
37.....41
five
42.....46
|