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 [email protected], " +
                "my father is [email protected], my mother is [email protected]";
        String regex = "\\w+@\\w+\\.\\w+";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        while(m.find()){
    
    
            System.out.println(m.group());

运行结果

[email protected]
[email protected]
[email protected]

猜你喜欢

转载自blog.csdn.net/younow22/article/details/112784546
今日推荐