[Java] simplifies the use of regular expressions, java Regular Expressions

Source: Internet
Author: User

[Java] simplifies the use of regular expressions, java Regular Expressions

 

Use

RegexString. (String). Pattern (Pattern). Start () +Subsequent operations (matches, find or replace)

 

Source code

Package com; import java. util. objects; import java. util. regex. matcher; import java. util. regex. pattern;/*** @ author YouXianMing1987@iCloud.com for simplified processing of Regular Expressions */public class RegexString {private String string; private Pattern pattern; private Matcher matcher; /// // /// Constructor ///////////////// //// *** Regular Expression object ** @ param str * String used for initialization */public RegexString (String str) {setString (Objects. requireNonNull (str ));} /// // Normal Method //////////////// /// // *** set the Regular Expression pattern ** @ param regex * Regular Expression statement * @ return RegexString */public RegexString pattern (String regex) {setPattern (Pattern. compile (regex); return this ;} /*** set the Regular Expression pattern ** @ param regex * Regular Expression statement * @ param flags * Regular Expression flag value * @ return RegexString */public RegexString pattern (String regex, int flags) {setPattern (Pattern. compile (regex, flags); return this;}/*** Regular Expression object start matching (this statement is required after pattern is set for subsequent operations) ** @ return RegexString */public RegexString start () {setMatcher (pattern. matcher (string); return this ;} /*** replace text ** @ param replacement * The replaced text * @ return */public String replace (String replacement) {return getMatcher (). replaceAll (replacement);}/*** determine whether to match (match all texts at a time, not step-by-step) ** @ return returns true if matching, and false if not matching. */public boolean matches () {return getMatcher (). matches ();}/*** determine whether to match (step-by-step match of text, please use it in conjunction with the while LOOP) ** @ return returns true if found, returns false if not found. */public boolean find () {return getMatcher (). after the find ();}/*** find () operation is successful, you can use matchString () to obtain the matched String ** @ return matched String */public String matchString () {return getMatcher (). after the group ();}/*** find () operation is successful, you can use matchStart () obtain the starting position of the matching ** @ return matched starting position */public int matchStart () {return getMatcher (). start ();}/*** after the find () operation is successful, you can use matchEnd () obtain the matched end position ** @ return start position */public int matchEnd () {return getMatcher (). end ();} /// // Static Method //////////////// ///// *** [static method] convenience constructor ** @ param str * String used for initialization * @ 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 ;}}

 

 

Example

Package com; public class Main {public static void main (String args []) {// search for 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 the 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 the spaces at the beginning and end of the string, and the extra 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 + "\"");}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.