Java简化正则表达式的使用(抽取工具类)

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

抽取如下工具类:

package com.js;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jiangshuai 用于简化处理正则表达式
 */
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.js;

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 + "\"");
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前言:本资源来自于javaeye,原资源链接地址:http://www.javaeye.com/topic/67398 原文如下: 以前写了一个java的正规表达式的java工具类,分享一下,有用到的欢迎下载使用。 如果你有常用的定义好的,且测试通过的正规表达式,欢迎跟贴,也让我享用一下 . 类中用到了 jakarta-oro-2.0.jar 包,请大家自己在 apache网站下下载 在这是junit测试单元类我就不提交了,在main()方法中有几个小测试,有兴趣自己玩吧. 这个工具类目前主要有25种正规表达式(有些不常用,但那时才仔细深入的研究了一下正规,写上瘾了,就当时能想到的都写了): 1.匹配图象; 2 匹配email地址; 3 匹配匹配并提取url ; 4 匹配并提取http ; 5.匹配日期 6 匹配电话; 7 匹配身份证 8 匹配邮编代码 9. 不包括特殊字符的匹配 (字符串中不包括符号 数学次方号^ 单引号' 双引号" 分号; 逗号, 帽号: 数学减号- 右尖括号> 左尖括号 0) 12 匹配正整数 13 匹配非正整数(负整数 + 0) 14 匹配负整数; 15. 匹配整数 ; 16 匹配非负浮点数(正浮点数 + 0) 17. 匹配正浮点数 18 匹配非正浮点数(负浮点数 + 0) 19 匹配负浮点数; 20 .匹配浮点数; 21. 匹配由26个英文字母组成的字符串; 22. 匹配由26个英文字母的大写组成的字符串 23 匹配由26个英文字母的小写组成的字符串 24 匹配由数字和26个英文字母组成的字符串; 25 匹配由数字、26个英文字母或者下划线组成的字符串; java源码: /* * Created on 2005-4-15 * * Summary of regular-expression constructs 正则表达式结构简介: * Construct Matches * Characters 字符: * x The character x x 字符 x * \\ The backslash character \\ 反斜杠 * \0n The character with octal value 0n (0 <= n <= 7) \0n 十进制数 (0 <= n <= 7) * \0nn The character with octal value 0nn (0 <= n <= 7) \0nn 十进制数 0nn (0 <= n <= 7) * \0mnn The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7) \0mnn 十进制数 0mnn (0 <= m <= 3, 0 <= n <= 7) * \xhh The character with hexadecimal value 0xhh \xhh 十六进制数 0x

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值