「Pattern类与Matcher类😋
」
美好一天从学习开始
❝「
❞Pattern 和 Matcher两个类是Java里专门用来模式匹配的(检索和指定模式匹配的字符序列)
」
「使用步骤」 「1.创建数据源」 将一个String对象确定为要进行检索的数据源
「2.建立Pattern 类对象」 Pattern pattern = Pattern.compile(regex); // 封装正则表达式
Pattern pattern = Pattern.compile(String regex,int flags); //flags设置匹配模式
flags有效值⬇️
「Pattern.CASE_INSENSITIVE、Pattern.MULTILINE、Pattern.DOTALL、Pattern.UNICODE_CASE、Pattern.CANON_EQ」
「3.得到matcher 对象」 Matcher matcher = pattern.matcher(String dataSource) // dataSource就是数据源
「pattern 调用 matcher() 方法返回一个 Matcher 类型对象」
「4.检索数据」 通过各种方法检索dataSource中的数据
🌟「Matcher类的常用方法」 「 public boolean find() 与 public boolean find(int start)
」
「寻找与regex匹配的下一个
子序列」「.start() // 子序列开始位置」 「.end() // ~结束位置」
「 public boolean lookingAt() // 判断是否有匹配的子序列(只看一次)
」「.start() // 子序列开始位置」 「.end() // ~结束位置」
「 public boolean matchers() // 是否完全匹配
」「 public String replaceAll(String replacement) // 返回一个所有能匹配到的子序列替换成replacement的新String对象
」「 public String replaceFirst(String replacement) // 和上面的类似,只不过只替换第一个
」
❤️ 欢迎你的到来! ❤️