Handwriting regular matching tools, convenient for later use, the free subsequent supplemented continuous optimization ....................
Constants class
public class RegexConstant { // matching In Email public static Final int E_MAIL = 0 ; // matching phone public static Final int MOBIL_PHONE =. 1 ; // matching telephone landline public static Final int TEL_PHONE = 2 ; // matching Zip code public static Final int ZIP_CODE = 3 ; // matches the URL public static Final int the URL =. 4 ; // matching format ip public static Final int the IP =. 5 ; // matching HTML tags public static Final int the HTML_TAG =. 6 ; // matching hex public static Final int the HEX =. 7 ; // matching the QQ public static Final int QQ =. 8 ; / / match the ID number public static Final int ID_CARD =. 9 ; // matching positive integer public static Final int POSITIVE_INTEGER = 10 ; }
Tools
Import java.util.regex.Matcher; Import java.util.regex.Pattern; public class RegexUtil { // regular expression string Private String = regexStr new new String (); // regular check, but does not create the object if incoming calls the regular expression method will be given public Boolean customMatcher (string TestStr) { // incoming character string to be detected the pattern = pattern of Pattern.compile (regexStr); Matcher Matcher = Pattern.matcher (TestStr); return matcher.matches (); } // regular expression matching common Private static Boolean genericMatcher(String regexExpre,String testStr){ Pattern pattern=Pattern.compile(regexExpre); Matcher matcher = pattern.matcher(testStr); return matcher.matches(); } //匹配器 public static boolean matcher(int RegexConstant_NAME,String testStr){ boolean flag=false; switch (RegexConstant_NAME){ case RegexConstant.E_MAIL:flag=genericMatcher("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*",testStr);break; case RegexConstant.HEX:flag=genericMatcher("/^#?([a-f0-9]{6}|[a-f0-9]{3})$/",testStr);break; case RegexConstant.IP:flag=genericMatcher("/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/",testStr);break; case RegexConstant.HTML_TAG:flag=genericMatcher("/^<([a-z]+)([^<]+)*(?:>(.*)<\\/\\1>|\\s+\\/>)$/",testStr);break; case RegexConstant.ZIP_CODE:flag=genericMatcher("/^[u4e00-u9fa5],{0,}$/",testStr);break; case RegexConstant.QQ:flag=genericMatcher("[1-9][0-9]{4,}",testStr);break; } return flag; } public RegexUtil(String regexStr) { this.regexStr = regexStr; } public String getRegexStr() { return regexStr; } public void setRegexStr(String regexStr) { this.regexStr = regexStr; } }
Currently the API
Requirement 1 : Use regular expressions and verify whether or not my custom to meet the requirements to be matched string
Method customMatcher (String testStr) match the custom regular
If you use a custom regular expression matching string matching, there is need to construct a reference to the incoming object of regular expressions to create RegexUtil, by calling the object matching string customMatcher (String testStr) passed to be matched , for the prevention of air pointer exception, this class does not provide the constructor with no arguments
System.out.println(new RegexUtil("[0-9]{3}").customMatcher("213"));
The result is true
Requirement 2 : Use Expression tools provide the matching string
genericMatcher (String regexExpre, String testStr) generic regular matcher
Only through . RegexUtil GenericMatcher (regexExpre String, String TestStr) can be realized string matching, the first parameter is a predefined constant, and the second expression is a string to be matched, such as
boolean reslut = RegexUtil.matcher(RegexConstant.E_MAIL, "[email protected]"); System.out.println(reslut);
The result is true
Follow-up needs to be improved in ....