1 matces() lookingAt() find()区别
  • matches方法尝试将整个输入序列与该模式匹配。

  • lookingAt尝试将输入序列从头开始与该模式匹配。

  • find方法扫描输入序列以查找与该模式匹配的下一个子序列。  

 // matches()对整个字符串进行匹配,只有整个字符串都匹配了才返回true

 

Pattern pattern = Pattern.compile(".*?o");
Matcher matcher = pattern.matcher("zoboco");

while(matcher.find()){
System.out.println(matcher.group(0));
}

运行结果:

zo
bo
co

 

java 正则表达式中的分组

正则表达式的分组在java中是怎么使用的. 
start(),end(),group()均有一个重载方法它们是start(int i),end(int i),group(int i)专用于分组操作,Mathcer类还有一个groupCount()用于返回有多少组. 
Java代码示例: 
Pattern p=Pattern.compile("([a-z]+)(\\d+)"); 匹配模式中一个括号就是一组
Matcher m=p.matcher("aaa2223bb"); 
m.find();   //匹配aaa2223 
m.groupCount();   //返回2,因为有2组 
m.start(1);   //返回0 返回第一组匹配到的子字符串在字符串中的索引号 
m.start(2);   //返回3 
m.end(1);   //返回3 返回第一组匹配到的子字符串的最后一个字符在字符串中的索引位置. 
m.end(2);   //返回7 
m.group();//返回aaa2223 m.group(1); //返回aaa,返回第一组匹配到的子字符串 m.group(2); //返回2223,返回第二组匹配到的子字符串

参考链接  https://www.cnblogs.com/dgwblog/p/10073256.html

相关文章:

  • 2021-12-13
  • 2021-12-13
  • 2021-08-09
  • 2022-01-04
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-03-07
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案