During the programming process, you often need to check the input data format. At this time, the regular expression is used. The data format is correct when the regular expression is matched, otherwise the format is wrong. In order to check whether the input data meets a certain format, you can use the Matches () method of the String class to judge. The syntax format is as follows:
BOOLEAN MATCHES (String Regex)
Regex: Specify regular expression.
Return value: Back Boolean type.
This method is used to inform whether the current string matches the regular expression specified by the parameter Regex. The return value is the BOOLEAN type. If the current string matches the regular expression, the method returns true, otherwise the False is returned.
Regular expressions are strings composed of characters with special meanings. These characters with special meanings are called metacharacters. The following lists some metacharacters of regular expressions. In the writing of regular expressions, the following should be The metacharacter is preceded by the "/" symbol for translation. For example, the metacharacter "/d" is "/d" in the regular expression, but for ".", it does not represent any character after translation, but Express a specific end.
.: Represents any character.
/D: Represents any number of 0-9.
/D: Represents any non -digital character.
/s: Represents blank characters.
/S: Represents non -blank characters.
/W: It means character that can be used as a identifier, but does not include "$".
/W: It means the character that cannot be used as a identifier.
/p {LOWER}: Represents a lowercase letter a ~ z.
/p {upper}: Represents uppercase letters a ~ a.
/p {ascii}: ascii character.
/p {alpha}: letter character.
/p {digit}: decimal number, 0-9.
/p {alnum}: numbers or letter characters.
/p {punch}: punctuation symbol.
/p {graph}: visible character.
/p {Print}: You can print characters.
/p {Blank}: blank or watchmaking.
/p {cntrl}: control character.
When using regular expressions, if a certain type of meta character is required multiple times, it is quite troublesome to enter one by one. At this time, you can use a limited character with regular expression to repeat the number. The common limited character and its meaning are listed below. .
?: 0 or 1 times.
*: 0 times or multiple times.
+: 0 or 1 times.
{n}: repeat N times.
{n,}: Repeat at least n times.
{n, m}: Repeat n ~ m times.
In regular expressions, you can also enclose multiple characters in square brackets. Various regular expressions in square brackets represent different meanings. The following lists the metacharacters and their meanings in square brackets.
[ABC]: Represents A, B, or C.
[^ABC]: Indicates any characters other than A, B, and C.
[A-Za-Z]: Any character of A ~ Z or A ~ Z.
[AD [mp]]: Any character of A ~ D or M ~ P.
[AZ && [DEF]]: D, E or F.
[az && [^bc]]]: A ~ z does not contain all characters of B and C between B and C.
[az && [^mp]]]: A ~ Z does not contain all characters of m ~ P between.
Example:
1. license plate number:
/*** * @description: Verify license plate number* @param carNum* Yu A106EK* @return Legal: true Illegal: false*/public static boolean validateCarNum(String carNum) {boole an result = false; string [] process = new String [] {"Jing", "Jin", "Ji", "Jin", "Liao", "Ji", "Black", "Shanghai", "Su", "Anhui", "Fujian", "Fujian", "Fujian", "Fujian", "Fujian", "Fujian", "Fujian", "Fujian" "Gan", "Lu", "Yu", "Hubei", "Xiang", "Guangdong", "Gui", "Qiong", "Chong", "Sichuan" "Tibetan", "Shaanxi", "Gan", "Qing", "Ning", "New", "Hong Kong", "Australia", "Mongolia"}; String reg = "/u4e00-/u9fa5] {1 } [Az] {1} [a-Z_0-9] {5} "; Boolean firstchar = false; if (carnum.length ()> 0) {firstchar = arrays.aslist (provente). CARNUM.SUBSTRING ( 0, 1);} Try {pattern p = pattern.compile (reg); matcher m = p.matcher (carnum); if (m.matches () &&FIRSTCHAR) {result = true;} Else {result = fals E ;}} catch (Exception e) {e.printStackTrace();}return result;}2. Mobile phone number:
/**** @Descripting: Verify the mobile phone number* @param mobilenum 15516985859* @Return Legal: True illegal: False*/Public Static Boolean ismobilenum (String Mobilenum) {B oolean result = false;try {Pattern p = Pattern.compile ("^((13 [0-9]) | (15 [^4, // d]) | (18 [0,5-9])) // d {8} $"); matcher m = p . Matcher (mobilenum); result = m.matches ();} Catch (Exception E) {e.printstacktrace ();} Return result;}Mobile phone number+fixed phone number: 010-1111111,15516985859,0377-1111111
// Java test whether it is a phone number (mobile phone, fixed telephone verification) string legalphone = "" "" "; string regexp ="^((13 [0-9]) | (15 [^4, // d]) | (18 [0,5-9])) // d {8} | [0] {1} [0-9] {2,3}-[0-9] {7,8} $ "; .compile (regexp); matcher m = p.Matcher (importpotentialbfos [i] .GetLegalphone ()); if (m.find ()) {// Note: m.find can only be used once, after the second call, all are used. For False Legalphone = ImportpotentialBFOS [i] .GetLegalphone (); uploadtmp.setlegalTelephone (Legalphone);} Else {Throw New BiZexception Error! ");}3. Real number:
String [] arrs = new string [] {"a", "1.123", "-1.23", "0", "+111"}; string regEx = "-? // d+// d* "; Pattern p = Pattern.Compile (regex); for (int i = 0; i <arRS.Length; i ++) {matcher m = p.Matcher (Arrs [i]); ]+":+m.matches ());}Print:
a: false
1.123: TRUE
-1.23: TRUE
0: TRUE
+111: false