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)文章
Java Swing窗體關(guān)閉事件的調(diào)用關(guān)系
這篇文章主要為大家詳細介紹了Java Swing窗體關(guān)閉事件的調(diào)用關(guān)系,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
淺談Spring Data JPA與MyBatisPlus的比較
本文主要介紹了淺談Spring Data JPA 與 MyBatisPlus的比較2024-08-08

