Java 簡化正則表達式的使用
使用
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)文章
詳解Springboot Oauth2 Server搭建Oauth2認(rèn)證服務(wù)
這篇文章主要介紹了Springboot Oauth2 Server 搭建Oauth2認(rèn)證服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
mybatis typeAliases 給實體類起別名的方法
這篇文章主要介紹了mybatis typeAliases 給實體類起別名,本文給大家分享兩種用法,通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
springboot實現(xiàn)https雙向傳輸協(xié)議的示例代碼
本文主要介紹了springboot實現(xiàn)https雙向傳輸協(xié)議的示例代碼,包含配置證書和私鑰路徑、調(diào)用請求方法等步驟,具有一定的參考價值,感興趣的可以了解一下2025-03-03
struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成簡單demo
本篇文章主要介紹了struts2.2.3+spring3.1.0 + mybatis3.1.0框架整合,結(jié)合在一起實現(xiàn)用戶的增刪改查功能,有需要的可以了解一下。2016-11-11
Java中l(wèi)ombok的@Builder注解的解析與簡單使用詳解
這篇文章主要介紹了Java中l(wèi)ombok的@Builder注解的解析與簡單使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
基于HTTP協(xié)議實現(xiàn)簡單RPC框架的方法詳解
RPC全名(Remote?Procedure?Call),翻譯過來就是遠程過程調(diào)用,本文將為大家介紹如何基于HTTP協(xié)議實現(xiàn)簡單RPC框架,感興趣的小伙伴可以了解一下2023-06-06

