Use
Regexstring.with (string). Pattern (pattern). Start () + subsequent operations (matches,find or replace)
Source
Packagecom;Importjava.util.Objects;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/** * @author[email protected] for simplifying the processing of regular expressions*/ Public classregexstring {Privatestring string; Privatepattern pattern; PrivateMatcher Matcher; //////////////////////Constructor////////////////////// /*** Regular Expression Object * *@paramSTR * Initialization string*/ Publicregexstring (String str) {setString (Objects.requirenonnull (str)); } //////////////////////Normal Method////////////////////// /*** Set the pattern of regular expressions * *@paramRegex * Regular expression statement *@returnregexstring*/ Publicregexstring pattern (String regex) {Setpattern (Pattern.compile (regex)); return This; } /*** Set the pattern of regular expressions * *@paramRegex * Regular expression statement *@paramflags * Regular Expression flag value *@returnregexstring*/ PublicRegexstring pattern (String regex,intflags) {Setpattern (Pattern.compile (regex, flags)); return This; } /*** Regular expression objects begin to match (this statement is required to perform subsequent operations after the pattern is set) * *@returnregexstring*/ Publicregexstring Start () {Setmatcher (Pattern.matcher (string)); return This; } /*** Replace text with * *@paramReplacement * Text to replace *@returnthe replaced string*/ Publicstring Replace (string replacement) {returnGetmatcher (). ReplaceAll (replacement); } /*** determine if match (all text at once, no step) * *@returnmatches the return true, and no match returns false. */ Public Booleanmatches () {returnGetmatcher (). matches (); } /*** Determine if the match is matched (step into the text, please use the while loop) * *@returnReturns True, no return false found. */ Public Booleanfind () {returnGetmatcher (). find (); } /*** When the Find () operation succeeds, the matching string can be obtained by matchstring () *@returnmatched string*/ PublicString matchstring () {returnGetmatcher (). Group (); } /*** When the Find () operation succeeds, you can get the starting position of the match by Matchstart () *@returnThe starting position of the match*/ Public intMatchstart () {returnGetmatcher (). Start (); } /*** When the Find () operation succeeds, the end position of the match can be obtained by matchend () *@returnThe starting position of the match*/ Public intMatchend () {returnGetmatcher (). end (); } //////////////////////Static Method////////////////////// /*** [static method] Convenient builder * *@paramSTR * Initialization String *@returnregexstring*/ Public Staticregexstring with (String str) {return Newregexstring (str); } //////////////////////Getter & Setter////////////////////// PublicString getString () {returnstring; } Public voidsetString (String string) { This. String =string; } PublicPattern Getpattern () {returnpattern; } Public voidSetpattern (pattern pattern) { This. Pattern =pattern; } PublicMatcher Getmatcher () {returnMatcher; } Public voidSetmatcher (Matcher Matcher) { This. Matcher =Matcher; }}
Example
Packagecom; Public classMain { Public Static voidMain (String args[]) {//Find Text{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 ()); } } //Match{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"); } } //Replace text{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")); } //remove whitespace from the end of the string and extra strings in the middle of the string{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 +" \ ""); } }}
[Java] simplifying the use of regular expressions