Java中Pattern、Matcher的用法,以及正则匹配邮箱的代码演示

  • java中有Pattern、Matcher类,主要作用于正则匹配

由官方文档可知,典型的调用序列为:

 Pattern p = Pattern.compile("a*b");
 Matcher m = p.matcher("aaaaab");
 boolean b = m.matches();

先将正则表达式转化成模式,再由模式用matcher方法使之成为Matcher匹配器,最后由Matcher匹配器调用一些方法,实现正则表达式的一些功能;

Matcher中变量和类型 方法 描述

  • Matcher appendReplacement​(StringBuffer sb, String replacement) 实现非终端附加和替换步骤。
  • Matcher appendReplacement​(StringBuilder sb, String replacement) 实现非终端附加和替换步骤。
  • StringBuffer appendTail​(StringBuffer sb) 实现终端附加和替换步骤。
  • StringBuilder appendTail​(StringBuilder sb) 实现终端附加和替换步骤。
  • int end() 返回最后一个字符匹配后的偏移量。
  • int end​(int group) 返回在上一个匹配操作期间由给定组捕获的子序列的最后一个字符之后的偏移量。
  • int end​(String name) 返回在上一个匹配操作期间由给定 named-capturing group捕获的子序列的最后一个字符之后的偏移量。
  • boolean find() 尝试查找与模式匹配的输入序列的下一个子序列。
  • boolean find​(int start) 重置此匹配器,然后尝试从指定的索引处开始查找与模式匹配的输入序列的下一个子序列。
  • String group() 返回上一个匹配项匹配的输入子序列。
  • String group​(int group) 返回上一个匹配操作期间给定组捕获的输入子序列。
  • String group​(String name) 返回在上一个匹配操作期间由给定 named-capturing group捕获的输入子序列。
  • int groupCount() 返回此匹配器模式中捕获组的数量。
  • boolean hasAnchoringBounds() 查询此匹配器的区域边界的锚定。
  • boolean hasTransparentBounds() 查询此匹配器的区域边界的透明度。
  • boolean hitEnd() 如果在此匹配器执行的最后一个匹配操作中搜索引擎命中输入结尾,则返回true。
  • boolean lookingAt() 尝试将从区域开头开始的输入序列与模式匹配。
  • boolean matches() 尝试将整个区域与模式匹配。
  • Pattern pattern() 返回此匹配器解释的模式。
  • static String quoteReplacement​(String s) 返回面值替换 String指定的 String 。
  • Matcher region​(int start, int end) 设置此匹配器区域的限制。
  • int regionEnd() 报告此匹配器区域的结束索引(不包括)。
  • int regionStart() 报告此匹配器区域的起始索引。
  • String replaceAll​(String replacement) 将具有给定替换字符串的模式匹配的输入序列的每个子序列替换。
  • String replaceAll​(Function<MatchResult,​String> replacer) 将与模式匹配的输入序列的每个子序列替换为将给定的替换函数应用于与该子序列对应的此匹配器的匹配结果的结果。
  • String replaceFirst​(String replacement) 将具有给定替换字符串的模式匹配的输入序列的第一个子序列替换。
  • String replaceFirst​(Function<MatchResult,​String> replacer) 将与模式匹配的输入序列的第一个子序列替换为将给定的replacer函数应用于与该子序列对应的此匹配器的匹配结果的结果。
  • boolean requireEnd() 如果更多输入可以将正匹配更改为负匹配,则返回true。
  • Matcher reset() 重置此匹配器。
  • Matcher reset​(CharSequence input) 使用新的输入序列重置此匹配器。
  • Stream results() 返回与模式匹配的输入序列的每个子序列的匹配结果流。
  • int start() 返回上一个匹配的起始索引。
  • int start​(int group) 返回上一个匹配操作期间给定组捕获的子序列的起始索引。
  • int start​(String name) 返回在上一个匹配操作期间由给定 named-capturing group捕获的子序列的起始索引。
  • MatchResult toMatchResult() 返回此匹配器的匹配状态为MatchResult 。
  • String toString() 返回此匹配器的字符串表示形式。
  • Matcher useAnchoringBounds​(boolean b) 设置此匹配器的区域边界的锚定。
  • Matcher usePattern​(Pattern newPattern) 更改 Pattern这 Matcher用来找到匹配。
  • Matcher useTransparentBounds​(boolean b) 设置此匹配器的区域边界的透明度。

声明方法的类 java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait


  • 以下是匹配邮箱的代码演示
String s = "My email num is hediya116@gmail.com, " +
                "my father is hasdqeq@162.com, my mother is 114324555@qq.com";
        String regex = "\\w+@\\w+\\.\\w+";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        while(m.find()){
            System.out.println(m.group());

运行结果

hediya116@gmail.com
hasdqeq@162.com
114324555@qq.com
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dylan、

耕码不易,白嫖可耻

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值