栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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());
    }


 String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,1,2,5-9])|(177))\d{8}$";

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135013.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号