欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 簡化正則表達(dá)式的使用

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

使用

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 用于簡化處理正則表達(dá)式
 */
public class RegexString {

  private String string;
  private Pattern pattern;
  private Matcher matcher;

  ////////////////////// Constructor //////////////////////

  /**
   * 正則表達(dá)式對象
   * 
   * @param str
   *      初始化用的字符串
   */
  public RegexString(String str) {
    setString(Objects.requireNonNull(str));
  }

  ////////////////////// Normal Method //////////////////////

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

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

  /**
   * 正則表達(dá)式對象開始匹配(設(shè)置完pattern后需要自行此語句才能做后續(xù)操作)
   * 
   * @return RegexString
   */
  public RegexString start() {
    setMatcher(pattern.matcher(string));
    return this;
  }

  /**
   * 進(jìn)行文本替換
   * 
   * @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í)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • Java之操作Redis案例講解

    Java之操作Redis案例講解

    這篇文章主要介紹了Java之操作Redis案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java Swing窗體關(guān)閉事件的調(diào)用關(guān)系

    Java Swing窗體關(guān)閉事件的調(diào)用關(guān)系

    這篇文章主要為大家詳細(xì)介紹了Java Swing窗體關(guān)閉事件的調(diào)用關(guān)系,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • java實(shí)現(xiàn)點(diǎn)擊按鈕事件彈出子窗口

    java實(shí)現(xiàn)點(diǎn)擊按鈕事件彈出子窗口

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)點(diǎn)擊按鈕事件彈出子窗口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Java中的泛型詳細(xì)解析

    Java中的泛型詳細(xì)解析

    這篇文章主要介紹了Java中的泛型詳細(xì)解析,泛型又稱參數(shù)化類型,是JDK5.0出現(xiàn)的新特性,解決了數(shù)據(jù)類型的安全型問題,Java泛型可以保證如果程序在編譯時(shí)沒用發(fā)出警告,運(yùn)行時(shí)就不會(huì)產(chǎn)生classCastException異常,需要的朋友可以參考下
    2024-01-01
  • 淺談Spring Data JPA與MyBatisPlus的比較

    淺談Spring Data JPA與MyBatisPlus的比較

    本文主要介紹了淺談Spring Data JPA 與 MyBatisPlus的比較
    2024-08-08
  • spring事物傳播propagation類別含義詳解

    spring事物傳播propagation類別含義詳解

    這篇文章主要介紹了spring事物傳播propagation類別含義詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java基礎(chǔ)之簡單介紹一下Maven

    Java基礎(chǔ)之簡單介紹一下Maven

    今天給大家復(fù)習(xí)一下Java基礎(chǔ)知識,簡單介紹Maven,文中有非常詳細(xì)的解釋,對Java初學(xué)者很有幫助喲,需要的朋友可以參考下
    2021-05-05
  • Java的List集合框架之Vector詳細(xì)解析

    Java的List集合框架之Vector詳細(xì)解析

    這篇文章主要介紹了Java的List集合框架之Vector詳細(xì)解析,List接口繼承Collection,Collection繼承于Iterable,List接口實(shí)現(xiàn)類分為Vector、ArrayList、LinkedList,Vector底層是一個(gè)Object數(shù)組,需要的朋友可以參考下
    2023-11-11
  • Java獲得元素屬性的注解信息的步驟

    Java獲得元素屬性的注解信息的步驟

    在Java編程中,注解是一種為代碼添加元數(shù)據(jù)的方式,通過反射機(jī)制,我們可以獲取元素屬性上的注解信息,這個(gè)過程對于框架開發(fā)和元數(shù)據(jù)處理非常有用,能夠?qū)崿F(xiàn)更靈活的功能,對java獲得元素屬性的注解信息相關(guān)知識感興趣的朋友一起看看吧
    2024-09-09
  • 如何在Spring中自定義scope的方法示例

    如何在Spring中自定義scope的方法示例

    這篇文章主要介紹了如何在Spring中自定義scope的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02

最新評論