Pattern和matcher
下面说一下这两个类的用法:
Pattern就是用于构建正则表达式的,正则表达式如果不懂的可以去我的上一篇博文看看.
1.构建一个Pattern:
static String str = "hello world";
static final Pattern p = Pattern.compile("he");
2.写一个方法:
View Code
1 public static void find(){ 2 Matcher m = p.matcher(str); 3 if(m.find()){ 4 //找到所需的字符串 5 //输出起始位置 6 System.out.println(m.start()); 7 //输出终止位置的下一位 8 System.out.println(m.end()); 9 } 10 else{ 11 //没有找到 12 System.out.println("could not find out"); 13 } 14 }
这里需要记得的就是end()方法返回的是终止位置的下一位,

浙公网安备 33010602011771号