Class Using the jakarta-oro-2.0.jar package, please download it on the Apache website
In this case, I will not submit the JUnit test unit class. There are several small tests in the main () method. If you are interested, try it yourself.
Currently, this tool class mainly has 25 types of Regular Expressions (some are not commonly used, but at that time, I carefully studied the regular expressions and wrote them all I could think of at that time ):
1. matching images; 2. Matching email addresses; 3. Matching and extracting URLs; 4. Matching and extracting HTTP;
5. Matching date 6 matching phone number; 7 matching ID card 8 matching zip code
9. match of special characters is not included (the character string does not include the symbol mathematical level number ^ single quotation marks 'double quotation marks "Semicolon; comma, Cap Number: mathematical minus signs-right angle brackets> left angle brackets <backslash/space, tab, carriage return, etc.
10 match non-negative integer (positive integer + 0) 11 match non-negative integer excluding zero (positive integer> 0)
12 match positive integer 13 match non-positive integer (negative integer + 0)
14. match a negative integer; 15. match an integer;
16 match non-negative floating point number (Positive floating point number + 0) 17. Match Positive floating point number
18 match non-Positive floating point number (negative floating point number + 0) 19 match negative floating point number;
20. Match floating point number; 21. match a string consisting of 26 English letters;
22. Match string 23 consisting of 26 letters in upper case to match string consisting of 26 letters in lower case
24. match a string consisting of digits and 26 English letters. 25. match a string consisting of digits, 26 English letters, or underscores;
Package com. ygj. util; </p> <p> import java. util. *; </p> <p> import org. apache. oro. text. regex. *; </p> <p>/** <br/> * class Introduction: use regular expressions to verify or extract data, the methods in the class are all static <br/> * main methods: 1. isHardRegexpValidate (String source, String regexp) <br/> batch configuration of case sensitive regular expressions * 2. isSoftRegexpValidate (String source, String regexp) <br/> * batch configuration of a regular case-insensitive expression <br/> * 3. getHardRegexpMatchResult (String source, String regexp) <br/> * return the batch configuration result set (case sensitive) Regular Expression batch) <br/> * 4. getSoftRegexpMatchResult (String source, String regexp) <br/> * return the batch configuration result set (a case-insensitive Regular Expression batch configuration) <br/> * 5 getHardRegexpArray (String source, String regexp) <br/> * return the batch configuration result set (case-sensitive regular expression batch configuration) <br/> * 6. getSoftRegexpMatchResult (String source, String regexp) <br/> * return the batch configuration result set required (a case-insensitive Regular Expression batch configuration) <br/> * 7. getBetweenSeparatorStr (final String originStr, final char leftSeparator, final char r IghtSeparator) <br/> * get the set of strings in the middle of the specified separator <br/> * @ mail wuzhi2000@hotmail.com <br/> * @ author ygj <br/> * <br/> */<br/> public final class Regexp <br/> {</p> <p>/** contains four groups of corresponding delimiters */<br/> static final Set SEPARATOR_SET = new TreeSet (); <br/>{< br/> SEPARATOR_SET.add ("("); <br/> SEPARATOR_SET.add (")"); <br/> SEPARATOR_SET.add ("["); <br/> SEPARATOR_SET.add ("]"); <br/> SEPARATOR_SET.add ("{"); <br/> SEPARATOR_SET.add ("}"); <br/> SEPARATOR_SET.add ("<"); <br/> SEPARATOR_SET.add ("> "); <br/>}</p> <p>/** stores various regular expressions (in the form of key-> value) */<br/> public static HashMap regexpHash = new HashMap (); </p> <p>/** stores various regular expressions (in the form of key-> value) */<br/> public static List matchingResultList = new ArrayList (); </p> <p> private Regexp () <br/>{</p> <p >}< br/>/** <br/> * return Regexp instance <br/> * @ return <br/> * /<br/> p Ublic static Regexp getInstance () <br/>{< br/> return new Regexp (); <br/>}</p> <p>/** <br/> * matched image </p> <p> * <br/> * format: /relative path/file name. suffix (Suffix: gif, dmp, png) <br/> * match: /forum/head_icon/admini20000000000ff.gif or admini20000001.dmp </p> <p> * <br/> * mismatch: c: /admins4512.gif <br/> */<br/> public static final String icon_regexp = "^ (/{0, 1}/w) {1 ,} //. (gif | dmp | png | jpg) $ | ^ // w {1 ,}//. (gif | dmp | png | Jpg) $ "; </p> <p>/** <br/> * matching email address </p> <p> * <br/> * format: XXX@XXX.XXX.XX <br/> * match: foo@bar.com or foobar@foobar.com.au </p> <p> * <br/> * mismatch: foo @ bar or $ @ bar.com <br/> */<br/> public static final String email_regexp = "(?: // W [-. _ // w] * // w @ // w [-. _ // w] * // w //. // w {2, 3} $ )"; </p> <p>/** <br/> * match and extract url </p> <p> * <br/> * Format: XXXX: // XXX. XXX. XXX. XX/XXX. XXX? XXX = XXX <br/> * match: http://www.suncer.com or news: // www </p> <p> * <br/> * extract (MatchResult matchResult = matcher. getMatch (): <br/> * matchResult. group (0) = http://www.suncer.com: 8080/index.html? Login = true <br/> * matchResult. group (1) = http <br/> * matchResult. group (2) = www.suncer.com <br/> * matchResult. group (3) =: 8080 <br/> * matchResult. group (4) =/index.html? Login = true <br/> * mismatch: c: /window <br/> */<br/> public static final String url_regexp = "(// w +): // ([^/:] +) (: // d *)? ([^ # // S] *) "; </p> <p>/** <br/> * match and extract http </p> <p> * <br/> * Format: http://XXX.XXX.XXX.XX/XXX.XXX? XXX = XXX or ftp://XXX.XXX.XXX or https: // XXX <br/> * match: http://www.suncer.com: 8080/index.html? Login = true </p> <p> * <br/> * extract (MatchResult matchResult = matcher. getMatch (): <br/> * matchResult. group (0) = http://www.suncer.com: 8080/index.html? Login = true <br/> * matchResult. group (1) = http <br/> * matchResult. group (2) = www.suncer.com <br/> * matchResult. group (3) =: 8080 <br/> * matchResult. group (4) =/index.html? Login = true <br/> * mismatch: news: // www <br/> */<br/> public static final String http_regexp = "(http | https | ftp ): // ([^/:] +) (: // d *)? ([^ # // S] *) "; </p> <p>/** <br/> * match date </p> <p> * <br/> * Format (the first digit is not 0 ): XXXX-XX-XX or xxxx xx or XXXX-X-X </p> <p> * <br/> * Range: 1900--2099 </p> <p> * <br/> * match: </p> <p> * <br/> * mismatch: 01-01-01 <br/> */<br/> public static final String date_regexp = "^ (19) {1} | (20) {1}) d {2}) | d {2 }) [-/s] {1} [01] {1} d {1} [-/s] {1} [0-3] {1} d {1} $ "; // match date </p> <p>/** <br/> * match phone number </p> <p> * <br/> * Format: 0XXX- XXXXXX (the first digit of 10-13 must be 0), 0XXX XXXXXXX (the first digit of 10-13 must be 0), or </p> <p> * (0XXX) XXXXXXXX (the first digit of 11-14 must be 0), XXXXXXXX (the first digit of 6-8 is not 0), or <br/> * XXXXXXXXXXX (the first digit of 11 is not 0) </p> <p> * <br/> * matching: 0371-123456 or (0371) 1234567 or (0371) 12345678 or 010-123456 or <br/> * 010-12345678 or 12345678912 </p> <p> * <br/> * mismatch: 1111-134355 or 0123456789 <br/> */<br/> public static final String phone_regexp = "^ (?: 0 [0-9] {2, 3} [-/s] {1} | // (0 [0-9] {2, 4 }//)) [0-9] {6, 8 }$ | ^ [1-9] {1} [0-9] {5, 7 }$ | ^ [1-9] {1} [0 -9] {10} $ "; </p> <p>/** <br/> * matched ID card </p> <p> * <br/> * Format: XXXXXXXXXX (10 digits) or XXXXXXXXXXXXX (13 BITs), XXXXXXXXXXXXXXX (15 bits), or <br/> * xxxxxxxxxxxxxxxx (18 bits) </p> <p> * <br/> * match: 0123456789123 </p> <p> * <br/> * mismatch: 0123456 <br/> */<br/> public static final String ID_card_regexp = "^ // d {10} | // d {13} | // d {15} | // D {18} $ "; </p> <p>/** <br/> * match the zip code </p> <p> * <br/> * Format: XXXXXX (6 bits) </p> <p> * <br/> * match: 012345 </p> <p> * <br/> * mismatch: 0123456 <br/> */<br/> public static final String ZIP_regexp = "^ [0-9] {6} $ "; // match the zip code </p> <p>/** <br/> * Does not include the matching of special characters (the character string does not include the symbol math character ^ single quotes 'double quotation marks" semicolon; comma, Cap: Mathematical minus signs-right angle brackets> left angle brackets <backslash/That is, space, tab, carriage return, etc) </p> <p> * <br/> * Format: x or a single character </p> <p> * <br/> * match: 012345 </p> <p> * <Br/> * mismatch: 0123456 <br/> */<br/> public static final String non_special_char_regexp = "^ [^ '/"//;, :-<> // s]. + $ "; // match the zip code </p> <p>/** <br/> * match a non-negative integer (positive integer + 0) <br/> */<br/> public static final String non_negative_integers_regexp = "^ // d + $ "; </p> <p>/** <br/> * match non-negative integers excluding zero (positive integers> 0) <br/> */<br/> public static final String non_zero_negative_integers_regexp = "^ [1-9] + // d * $ "; </p> <p>/** <br /> * <Br/> * match a positive integer <br/> */<br/> public static final String positive_integer_regexp = "^ [0-9] * [1-9] [0-9] * $ "; </p> <p>/** <br/> * match non-positive integers (negative integers + 0) <br/> */<br/> public static final String non_positive_integers_regexp = "^ (-/d +) | (0 +) $ "; </p> <p>/** <br/> * match a negative integer <br/> */<br/> public static final string negative_integers_regexp = "^-[0-9] * [1-9] [0-9] * $ "; </ P> <p>/** <br/> * matched integer <br/> */<br/> public static final String integer_regexp = "^ -? // D + $ "; </p> <p>/** <br/> * match non-negative floating point number (Positive floating point number + 0) <br/> */<br/> public static final String non_negative_rational_numbers_regexp = "^ // d + (//. // d + )? $ "; </P> <p>/** <br/> * match the Positive floating point number <br/> */<br/> public static final String positive_rational_numbers_regexp = "^ ([0-9] + //. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * //. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "; </p> <p>/** <br/> * match a non-Positive floating point number (negative floating point number + 0) <br/> */<br/> public static final String non_positive_rational_numbers_regexp = "^ (-/d + (//. // d + )?) | (0 + (//. 0 + )?)) $ "; </P> <p>/** <br/> * match a negative floating point number <br/> */<br/> public static final String negative_rational_numbers_regexp = "^ (-([0-9] + //. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * //. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "; </p> <p>/** <br/> * match floating point number <br/> */<br/> public static final string rational_numbers_regexp = "^ (-? // D +) (//. // d + )? $ "; </P> <p>/** <br/> * match a string consisting of 26 English letters. <br/> */ <br/> public static final String letter_regexp = "^ [A-Za-z] + $ "; </p> <p>/** <br/> * match a string consisting of 26 uppercase letters. <br/> * <br/> */<br/> public static final String upward_letter_regexp = "^ [A-Z] + $ "; </p> <p>/** <br/> * match a string consisting of 26 lowercase letters. <br/> * <br/> */<br/> public static final String lower_letter_regexp = "^ [a-z] + $ "; </p> <p >/** <Br/> * match a string consisting of a number and 26 English letters. <br/> */<br/> public static final String letter_number_regexp = "^ [A-Za-z0-9] + $ "; </p> <p>/** <br/> * match a string consisting of digits, 26 English letters, or underscores <br/> * <br /> */<br/> public static final String letter_number_underline_regexp = "^/w + $ "; </p> <p>/** <br/> * Add Regular Expressions (stored as key-> value) <br/> * @ param regexpName <br/> * Name of the regular expression '<br/> * @ param regexp <Br/> * content of this regular expression <br/> */<br/> public void putRegexpHash (String regexpName, String regexp) <br/> {<br/> regexpHash. put (regexpName, regexp ); <br/>}</p> <p>/** <br/> * obtain the content of a regular expression (extract value using the key name [content of a regular expression]) <br/> * @ param regexpName <br/> * Regular Expression name <br/> * @ return regular expression content <br/> */<br/> public String getRegexpHash (String regexpName) <br/>{< br/> if (regexpHash. get (regexpName )! = Null) <br/>{< br/> return (String) regexpHash. get (regexpName); <br/>}< br/> else <br/>{< br/> System. out. println ("this regular expression is not available in regexpHash"); <br/> return ""; <br/>}</p> <p>/** <br/> * clear the regular expression storage unit <br/> */<br/> public void clearRegexpHash () <br/>{< br/> regexpHash. clear (); <br/> return; <br/>}</p> <p>/** <br/> * batch configuration of case-sensitive regular expressions <br/> * @ param source <br/> * Source string of the batch configuration <br/> * @ param regexp <br/> * Regular Expression of the batch configuration <br/> * <br /> * @ return: returns true if the source string meets the requirements, otherwise, the system returns a false value, for example, Regexp. isHardRegexpValidate ("ygj@suncer.com.cn", email_regexp) returns true <br/> */<br/> public static boolean isHardRegexpValidate (String source, String regexp) <br/>{</p> <p> try <br/> {<br/> // defines the template type of a regular expression object. <br/> PatternCompiler compiler = new Perl5Compiler (); </p> <p> // Regular Expression compares batch objects <br/> PatternMatcher matcher = new Perl5Matcher (); </p> <p> // regular expression template with case-sensitive instance size <br/> Pattern hardPattern = compiler. compile (regexp); </p> <p> // return the batch configuration result <br/> return matcher. contains (source, hardPattern); </p> <p >}< br/> catch (MalformedPatternException e) <br/>{< br/> e. printStackTrace (); </p> <p >}< br/> return false; <br/>}</p> <p>