查看“正则表达式 Matcher 类”的源代码
←
正则表达式 Matcher 类
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
[https://www.bilibili.com/video/BV1Eq4y1E79W/?p=18 https://www.bilibili.com/video/BV1Eq4y1E79W/?p=19] Matcher 对象是对输入字符串进行解释和匹配的引擎。 与 Pattern 类一样,Matcher 也没有公共构造方法。 需要调用 Pattern 对象的 matcher 方法来获得1个 Matcher 对象。 === 方法一览 === {| class="wikitable" !方法 !说明 |- |public int start() |返回以前匹配的初始索引 |- |public int start(int group) |返回在以前的匹配操作期间,由给定组所捕获的子序列的初始索引 |- |public int end() |返回最后匹配字符之后的偏移量 |- |public int end(int group) |返回在以前的匹配操作期间,由给定组所捕获子序列的最后字符之后的偏移量 |- |public boolean lookingAt() |尝试将从区域开头开始的输入序列与该模式匹配 |- |public boolean find() |尝试查找与该模式匹配的输入序列的下一个子序列 |- |public boolean find(int start) |重置此匹配器,然后尝试查找匹配该模式、从指定索引开始的输入序列的下一个子序列 |- |public boolean matches() |尝试将整个区域与模式匹配 |- |public Matcher appendReplacement(StringBuffer sb, String replacement) |实现非终端添加和替换步骤 |- |public StringBuffer appendTail(StringBuffer sb) |实现终端添加和替换步骤 |- |public String replaceAll(String replacement) |替换模式与给定替换字符串相匹配的输入序列的每个子序列 |- |public String replaceFirst(String replacement) |替换模式与给定替换字符串匹配的输入序列的第一个子序列 |- |public static String quoteReplacement(String s) |返回指定字符串的字面替换字符串。 这个方法返回一个字符串,就像传递给 Matcher 类的 appendReplacement 方法一个字面字符串一样工作 |- |public Matcher appendReplacement(StringBuffer sb, String replacement) |实现非终端添加和替换步骤 |- |public StringBuffer appendTail(StringBuffer sb) |实现终端添加和替换步骤 |} === matcher.start() 和 matcher.end() === ==== 示例1: ==== <syntaxhighlight lang="java"> import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherMethod { public static void main(String[] args) { String content = "hello edu jack tom hello smith hello"; String regStr = "hello"; Pattern pattern = Pattern.compile(regStr); Matcher matcher = pattern.matcher(content); while (matcher.find()) { System.out.println("=============================="); System.out.println("matcher.start():" + matcher.start()); System.out.println("matcher.end():" + matcher.end()); } } } </syntaxhighlight><syntaxhighlight lang="console"> ============================== matcher.start():0 matcher.end():5 ============================== matcher.start():19 matcher.end():24 ============================== matcher.start():31 matcher.end():36 </syntaxhighlight>start 和 end 返回的是每一次匹配后 substring 的 fromIndex include 和 endIndex exclude ==== 示例2: ==== <syntaxhighlight lang="java"> import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherMethod { public static void main(String[] args) { String content = "hello edu jack tom hello smith hello"; String regStr = "hello"; Pattern pattern = Pattern.compile(regStr); Matcher matcher = pattern.matcher(content); while (matcher.find()) { System.out.println("=============================="); System.out.println("matcher.start():" + matcher.start()); System.out.println("matcher.end():" + matcher.end()); System.out.println("找到:" + content.substring(matcher.start(), matcher.end())); } } } </syntaxhighlight><syntaxhighlight lang="console"> ============================== matcher.start():0 matcher.end():5 找到:hello ============================== matcher.start():19 matcher.end():24 找到:hello ============================== matcher.start():31 matcher.end():36 找到:hello </syntaxhighlight> === matcher.matches() 整体匹配 === 整体匹配方法,常用于校验某个字符串是否满足某个规则 ==== 示例1: ==== <syntaxhighlight lang="java"> import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherMethod { public static void main(String[] args) { String content = "hello edu jack tom hello smith hello"; String regStr = "hello"; Pattern pattern = Pattern.compile(regStr); Matcher matcher = pattern.matcher(content); //整体匹配方法,常用于校验某个字符串是否满足某个规则 System.out.println("matcher.matches():" + matcher.matches()); } } </syntaxhighlight><syntaxhighlight lang="console"> matcher.matches():false </syntaxhighlight> ==== 示例2: ==== <syntaxhighlight lang="java"> import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherMethod { public static void main(String[] args) { String content = "hello edu jack tom hello smith hello"; String regStr = "hello.*"; Pattern pattern = Pattern.compile(regStr); Matcher matcher = pattern.matcher(content); //整体匹配方法,常用于校验某个字符串是否满足某个规则 System.out.println("matcher.matches():" + matcher.matches()); } } </syntaxhighlight><syntaxhighlight lang="console"> matcher.matches():true </syntaxhighlight> === matcher.replaceAll(String replacement) 正则表达式匹配替换 === <syntaxhighlight lang="java"> import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherMethod { public static void main(String[] args) { String content = "hello edu jack hspedutom hello smith hello hspedu hspedu"; String regStr = "hspedu"; Pattern pattern = Pattern.compile(regStr); Matcher matcher = pattern.matcher(content); //完成如果 content 有 hspedu 就替换成 韩顺平教育 String newContent = matcher.replaceAll("韩顺平教育"); //注意:返回的字符串才是替换后的字符串 System.out.println("content:" + content); System.out.println("newContent:" + newContent); } } </syntaxhighlight><syntaxhighlight lang="console"> content:hello edu jack hspedutom hello smith hello hspedu hspedu newContent:hello edu jack 韩顺平教育tom hello smith hello 韩顺平教育 韩顺平教育 </syntaxhighlight>
返回至
正则表达式 Matcher 类
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息