正则表达式基础应用(获取Pattern)

前端之家收集整理的这篇文章主要介绍了正则表达式基础应用(获取Pattern)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/* * 将字符串中符合规则的子串取出 * * 操作步骤: * 1.先将正则表达式封装成对象 * 2.让正则对象和要操作的字符串相关联 * 3.关联后获取正则匹配引擎 * 4.通过引擎对符合规则的子串进行操作,比如取出 * */ package com.it; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyRegex { public static void main(String[] args) { String str = "esdf 23fd jlwle sdf dafk dfk fd adf"; String regex = "\\b[a-z]{3}\\b"; // 此处\b为边界匹配 //将规则封装成Pattern对象 Pattern p = Pattern.compile(regex); //让Pattern对象和要作用字符串关联,获取匹配器对象 Matcher m = p.matcher(str); while(m.find()){// find()的作用是把规则作用在字符串上 System.out.println(m.group()); //System.out.println(m.start()+"..."+m.end()); } } } 原文链接:https://www.f2er.com/regex/361038.html

猜你在找的正则表达式相关文章