最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 簡化正則表達式的使用

 更新時間:2017年04月29日 12:03:53   作者:YouXianMing  
本篇文章主要介紹了Java 簡化正則表達式使用的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧

使用

RegexString.with(string).pattern(pattern).start() + 后續(xù)操作(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 //////////////////////

  /**
   * 設(shè)置正則表達式的pattern
   * 
   * @param regex
   *      正則表達式語句
   * @return RegexString
   */
  public RegexString pattern(String regex) {
    setPattern(Pattern.compile(regex));
    return this;
  }

  /**
   * 設(shè)置正則表達式的pattern
   * 
   * @param regex
   *      正則表達式語句
   * @param flags
   *      正則表達式flag值
   * @return RegexString
   */
  public RegexString pattern(String regex, int flags) {
    setPattern(Pattern.compile(regex, flags));
    return this;
  }

  /**
   * 正則表達式對象開始匹配(設(shè)置完pattern后需要自行此語句才能做后續(xù)操作)
   * 
   * @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();
  }

  /**
   * 判斷是否匹配(分步匹配文本,請結(jié)合while循環(huán)使用)
   * 
   * @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()獲取匹配的結(jié)束位置
   * 
   * @return 匹配的起始位置
   */
  public int matchEnd() {
    return getMatcher().end();
  }

  ////////////////////// Static Method //////////////////////

  /**
   * [靜態(tài)方法] 便利構(gòu)造器
   * 
   * @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 + "\"");
    }
  }
}

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論

三河市| 武隆县| 永泰县| 潮州市| 桑植县| 安新县| 两当县| 越西县| 客服| 将乐县| 来凤县| 婺源县| 潜江市| 阳高县| 泸定县| 环江| 遂溪县| 视频| 务川| 涡阳县| 德兴市| 云霄县| 武穴市| 平定县| 清原| 嵊州市| 延吉市| 白山市| 家居| 石屏县| 定边县| 临湘市| 保山市| 烟台市| 读书| 铜梁县| 明溪县| 镇宁| 汶上县| 长宁区| 大连市|