博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Java] 简化正则表达式的使用
阅读量:6944 次
发布时间:2019-06-27

本文共 4310 字,大约阅读时间需要 14 分钟。

 

使用

RegexString.with(string).pattern(pattern).start() + 后续操作(matches,find或者是replace)

 

源码

package com;import java.util.Objects;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @author YouXianMing1987@iCloud.com 用于简化处理正则表达式 */public class RegexString {    private String string;    private Pattern pattern;    private Matcher matcher;    // Constructor //    /**     * 正则表达式对象     *      * @param str     *            初始化用的字符串     */    public RegexString(String str) {        setString(Objects.requireNonNull(str));    }    // Normal Method //    /**     * 设置正则表达式的pattern     *      * @param regex     *            正则表达式语句     * @return RegexString     */    public RegexString pattern(String regex) {        setPattern(Pattern.compile(regex));        return this;    }    /**     * 设置正则表达式的pattern     *      * @param regex     *            正则表达式语句     * @param flags     *            正则表达式flag值     * @return RegexString     */    public RegexString pattern(String regex, int flags) {        setPattern(Pattern.compile(regex, flags));        return this;    }    /**     * 正则表达式对象开始匹配(设置完pattern后需要自行此语句才能做后续操作)     *      * @return RegexString     */    public RegexString start() {        setMatcher(pattern.matcher(string));        return this;    }    /**     * 进行文本替换     *      * @param replacement     *            用来替换的文本     * @return 替换后的字符串     */    public String replace(String replacement) {        return getMatcher().replaceAll(replacement);    }    /**     * 判断是否匹配(一次性匹配全部文本,不分步)     *      * @return 匹配了返回true,没有匹配返回false.     */    public boolean matches() {        return getMatcher().matches();    }    /**     * 判断是否匹配(分步匹配文本,请结合while循环使用)     *      * @return 找到了返回true,没有找到返回false.     */    public boolean find() {        return getMatcher().find();    }    /**     * find()操作成功后,可以通过matchString()获取匹配的字符串     *      * @return 匹配的字符串     */    public String matchString() {        return getMatcher().group();    }    /**     * find()操作成功后,可以通过matchStart()获取匹配的起始位置     *      * @return 匹配的起始位置     */    public int matchStart() {        return getMatcher().start();    }    /**     * find()操作成功后,可以通过matchEnd()获取匹配的结束位置     *      * @return 匹配的起始位置     */    public int matchEnd() {        return getMatcher().end();    }    // Static Method //    /**     * [静态方法] 便利构造器     *      * @param str     *            初始化用的字符串     * @return RegexString     */    public static RegexString with(String str) {        return new RegexString(str);    }    // Getter & Setter //    public String getString() {        return string;    }    public void setString(String string) {        this.string = string;    }    public Pattern getPattern() {        return pattern;    }    public void setPattern(Pattern pattern) {        this.pattern = pattern;    }    public Matcher getMatcher() {        return matcher;    }    public void setMatcher(Matcher matcher) {        this.matcher = matcher;    }}

 

 

示例

package com;public class Main {    public static void main(String args[]) {        // 查找文本        {            String src = "This is my small example string which I'm going to use for pattern matching.";            RegexString string = RegexString.with(src).pattern("\\w+").start();            while (string.find()) {                System.out.println(string.matchStart() + "," + string.matchEnd() + " : " + string.matchString());            }        }                // 匹配        {            String src = "This is my small example string which I'm going to use for pattern matching.";            if (RegexString.with(src).pattern("^This.+$").start().matches()) {                System.out.println("Yes");            }        }        // 替换文本        {            String src = "This is my small example string which I'm going to use for pattern matching.";            System.out.println(RegexString.with(src).pattern("\\w+").start().replace("Regex"));        }        // 去掉字符串首尾的空格,以及字符串中间多余的字符串        {            String src = "   This    is    my small example string    which  I'm    going to    use  for  pattern  matching.     ";            String tmp = RegexString.with(src).pattern("^\\s+|\\s+$").start().replace("");            String des = RegexString.with(tmp).pattern("\\s+").start().replace(" ");            System.out.println("\"" + des + "\"");        }    }}

 

转载地址:http://tnanl.baihongyu.com/

你可能感兴趣的文章
解决已经装了telnet还是无法使用的问题
查看>>
最新变体后缀YOUR_LAST_CHANCE勒索病毒分析处理数据恢复,扩展名firex3m系列
查看>>
欧盟公布人工智能道德准则 列出可信赖AI关键条件
查看>>
VS2019生成的DeBug版程序寻找main函数
查看>>
如何实现分享网站文章到微信朋友圈时显示指定缩略图或LOGO
查看>>
oracle技术之利用STANDBY将单实例数据库升级为RAC环境(三)
查看>>
浅谈如何管理Oracle PGA
查看>>
设置grep高亮显示匹配项
查看>>
简单分析阿里云生态环境
查看>>
ambari 离线安装 HDP-2.3 集群
查看>>
C++ STL map(映射)
查看>>
hbase性能优化
查看>>
Spring中使用注解 @Scheduled执行定时任务
查看>>
hadoop的常用shell命令
查看>>
关于我用过Matlab后的一些看法
查看>>
一个优秀的项目经理应该具备的七个特点
查看>>
iOS中Block的基础用法
查看>>
nagios安装配置(一):nagios
查看>>
10个必备的移动UI设计资源站(转)
查看>>
linux,SSL的实现过程
查看>>