当前位置: 移动技术网 > IT编程>开发语言>Java > Java 简化正则表达式的使用

Java 简化正则表达式的使用

2019年07月22日  | 移动技术网IT编程  | 我要评论

使用

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网