Java 简化正则表达式的使用
使用
RegexString.with(string).pattern(pattern).start()+后续操作(matches,find或者是replace)
源码
packagecom;
importjava.util.Objects;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
/**
*@authorYouXianMing1987@iCloud.com用于简化处理正则表达式
*/
publicclassRegexString{
privateStringstring;
privatePatternpattern;
privateMatchermatcher;
//////////////////////Constructor//////////////////////
/**
*正则表达式对象
*
*@paramstr
*初始化用的字符串
*/
publicRegexString(Stringstr){
setString(Objects.requireNonNull(str));
}
//////////////////////NormalMethod//////////////////////
/**
*设置正则表达式的pattern
*
*@paramregex
*正则表达式语句
*@returnRegexString
*/
publicRegexStringpattern(Stringregex){
setPattern(Pattern.compile(regex));
returnthis;
}
/**
*设置正则表达式的pattern
*
*@paramregex
*正则表达式语句
*@paramflags
*正则表达式flag值
*@returnRegexString
*/
publicRegexStringpattern(Stringregex,intflags){
setPattern(Pattern.compile(regex,flags));
returnthis;
}
/**
*正则表达式对象开始匹配(设置完pattern后需要自行此语句才能做后续操作)
*
*@returnRegexString
*/
publicRegexStringstart(){
setMatcher(pattern.matcher(string));
returnthis;
}
/**
*进行文本替换
*
*@paramreplacement
*用来替换的文本
*@return替换后的字符串
*/
publicStringreplace(Stringreplacement){
returngetMatcher().replaceAll(replacement);
}
/**
*判断是否匹配(一次性匹配全部文本,不分步)
*
*@return匹配了返回true,没有匹配返回false.
*/
publicbooleanmatches(){
returngetMatcher().matches();
}
/**
*判断是否匹配(分步匹配文本,请结合while循环使用)
*
*@return找到了返回true,没有找到返回false.
*/
publicbooleanfind(){
returngetMatcher().find();
}
/**
*find()操作成功后,可以通过matchString()获取匹配的字符串
*
*@return匹配的字符串
*/
publicStringmatchString(){
returngetMatcher().group();
}
/**
*find()操作成功后,可以通过matchStart()获取匹配的起始位置
*
*@return匹配的起始位置
*/
publicintmatchStart(){
returngetMatcher().start();
}
/**
*find()操作成功后,可以通过matchEnd()获取匹配的结束位置
*
*@return匹配的起始位置
*/
publicintmatchEnd(){
returngetMatcher().end();
}
//////////////////////StaticMethod//////////////////////
/**
*[静态方法]便利构造器
*
*@paramstr
*初始化用的字符串
*@returnRegexString
*/
publicstaticRegexStringwith(Stringstr){
returnnewRegexString(str);
}
//////////////////////Getter&Setter//////////////////////
publicStringgetString(){
returnstring;
}
publicvoidsetString(Stringstring){
this.string=string;
}
publicPatterngetPattern(){
returnpattern;
}
publicvoidsetPattern(Patternpattern){
this.pattern=pattern;
}
publicMatchergetMatcher(){
returnmatcher;
}
publicvoidsetMatcher(Matchermatcher){
this.matcher=matcher;
}
}
示例
packagecom;
publicclassMain{
publicstaticvoidmain(Stringargs[]){
//查找文本
{
Stringsrc="ThisismysmallexamplestringwhichI'mgoingtouseforpatternmatching.";
RegexStringstring=RegexString.with(src).pattern("\\w+").start();
while(string.find()){
System.out.println(string.matchStart()+","+string.matchEnd()+":"+string.matchString());
}
}
//匹配
{
Stringsrc="ThisismysmallexamplestringwhichI'mgoingtouseforpatternmatching.";
if(RegexString.with(src).pattern("^This.+$").start().matches()){
System.out.println("Yes");
}
}
//替换文本
{
Stringsrc="ThisismysmallexamplestringwhichI'mgoingtouseforpatternmatching.";
System.out.println(RegexString.with(src).pattern("\\w+").start().replace("Regex"));
}
//去掉字符串首尾的空格,以及字符串中间多余的字符串
{
Stringsrc="ThisismysmallexamplestringwhichI'mgoingtouseforpatternmatching.";
Stringtmp=RegexString.with(src).pattern("^\\s+|\\s+$").start().replace("");
Stringdes=RegexString.with(tmp).pattern("\\s+").start().replace("");
System.out.println("\""+des+"\"");
}
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!