网络编程
位置:首页>> 网络编程>> 网络编程>> Java正则表达式Pattern和Matcher原理详解

Java正则表达式Pattern和Matcher原理详解

作者:yaominghui  发布时间:2023-01-10 14:01:31 

标签:java,Pattern,Matcher,正则

这篇文章主要介绍了Java正则表达式Pattern和Matcher原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

基本使用

Scanner中的使用正则表达式


//Scanner 支持的分组
   Scanner cin=new Scanner("red a bbc").useDelimiter("\\s*a\\s*");
   System.out.println(cin.next());
   System.out.println(cin.next());out:
redbbc

等同于下面代码


//等于 正则
Scanner cin2=new Scanner("red a bbc");
cin2.findInLine("\\s*"); // findLine 允许存在多个,match()为最终需要匹配的字符串
MatchResult result = cin2.match();
for (int i = 0; i < result.groupCount(); i++) {
 System.out.println(result.group(i));
}

Pattern:


//基本匹配
   boolean b = Pattern.matches("a*b", "aaaab");
   System.out.println(b);

String的aplit的实现


//按照数字分割
   Pattern p=Pattern.compile("\\d+");
   String[] str=p.split("好456456像:0532214是");
   for (int i = 0; i < str.length; i++) {
     System.out.println(str[i]);

}

一般使用Pattern.matches(String regex,CharSequence input)是一个静态方法,用于快速匹配字符串,该方法适合用于只匹配一次,且匹配全部字符串.

Java代码示例:


Pattern.matches("\\d+","2223");//返回true
Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,这里aa不能匹配到
Pattern.matches("\\d+","22bb23");//返回false,需要匹配到所有字符串才能返回true,这里bb不能匹配到 Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("22bb23"); m.pattern();//返回p 也就是返回该Matcher对象是由哪个Pattern对象的创建的

重点:

matches 方法尝试将整个输入序列与该模式匹配。

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

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


// matches()对整个字符串进行匹配,只有整个字符串都匹配了才返回true
   Pattern p=Pattern.compile("\\d+");
   Matcher m=p.matcher("22bb23");
   m.matches();//返回false,因为bb不能被\d+匹配,导致整个字符串匹配未成功.
   Matcher m2=p.matcher("2223");
   m2.matches();//返回true,因为\d+匹配到了整个字符串

// lookingAt()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
   Pattern p1=Pattern.compile("\\d+");
   Matcher m3=p1.matcher("22bb23");
   m.lookingAt();//返回true,因为\d+匹配到了前面的22
   Matcher m4=p1.matcher("aa2223");
   m2.lookingAt();//返回false,因为\d+不能匹配前面的aa

// find()对字符串进行匹配,匹配到的字符串可以在任何位置.
   Pattern p2=Pattern.compile("\\d+");
   Matcher m5=p2.matcher("22bb23");
   m.find();//返回true
   Matcher m6=p2.matcher("aa2223");
   m2.find();//返回true
   Matcher m7=p2.matcher("aa2223bb");
   m3.find();//返回true
   Matcher m8=p2.matcher("aabb");
   m4.find();//返回false

Mathcer.start()/ Matcher.end()/ Matcher.group()

当使用matches(),lookingAt(),find()执行匹配操作后,就可以利用以上三个方法得到更详细的信息.

  • start()返回匹配到的子字符串在字符串中的索引位置.

  • end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置. 即为最后位置加一

  • group()返回匹配到的子字符串

Java代码示例:


Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("aaa2223bb");
m.find();//匹配2223
m.start();//返回3
m.end();//返回7,返回的是2223后的索引号
m.group();//返回2223

Mathcer m2=p.matcher("2223bb");
m2.lookingAt();  //匹配2223
m2.start();  //返回0,由于lookingAt()只能匹配前面的字符串,所以当使用lookingAt()匹配时,start()方法总是返回0
m2.end();  //返回4
m2.group();  //返回2223

Matcher m3=p.matcher("2223"); //如果Matcher m3=p.matcher("2223bb"); 那么下面的方法出错,因为不匹配返回false
m3.matches();  //匹配整个字符串
m3.start();  //返回0
m3.end();  //返回3,原因相信大家也清楚了,因为matches()需要匹配所有字符串
m3.group();  //返回2223

说了这么多,相信大家都明白了以上几个方法的使用,该说说正则表达式的分组在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(1);  //返回aaa,返回第一组匹配到的子字符串
m.group(2);  //返回2223,返回第二组匹配到的子字符串

验证手机号


// 验证手机号
   Pattern compile = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
   Matcher matcher1 = compile.matcher("15071089603");
   while(matcher1.find()){
     System.out.println(matcher1.group());
   }

/**
  * 验证手机号码
  *
  * 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147、182
  * 联通号码段:130、131、132、136、185、186、145
  * 电信号码段:133、153、180、189、177
  *
  */
String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,1,2,5-9])|(177))\\d{8}$";

来源:https://www.cnblogs.com/dgwblog/p/10073256.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com