6. Using Regular expressions
A regular expression is a method of describing a set of strings, based on the common characteristics of each string in a collection of strings. In program development, regular expressions are widely used in string
Find, replace, match, and so on, so the flexibility to use regular expressions is very important to programmers. Regular expressions contain characters with special meanings, which are called meta-characters of regular expressions.
The characters in the regular expression and their meanings are as follows:
Meta-character regular The meaning of the expression. "."represents any one character
\d "\\d" represents any number of 0~9\d "\\d" represents any non-numeric character \s "\\s" represents a white space character, such as "\ T", "\ n"
\s "\\s" stands for non-whitespace characters
\w "\\w" represents a character that is not available for identifiers
\p{lower} "\\p{lower}" stands for lowercase letter {a~z}
\p{upper} "\\p{upper}" represents capital letter {A~z}
\P{ASCII} "\\p{ascii}" ASCII character
\p{alpha} "\\p{alpha}" alphabetic character
\p{digit} "\\p{digit}" decimal number, i.e. [0~9]
\p{alnum} "\\p{alnum}" numeric or alphabetic characters
\P{PUNCT} "\\p{punct}" punctuation:! " #%& ' () *+,-_/:;<=>[email protected][\]^_ ' {|} ~
\p{graph} "\\p{graph}" visible character: [\p{alnum}\p{punct}]
\p{print} "\\p{print}" printable character: [\p{graph}\x20]
\p{blank} "\\p{blank}" space or tab: [\ t]
\p{cntrl} "\\p{cntrl}" control character: [\x00-\x1f\x7f]
Description: In the regular expression, "." Represents any one character, so if you want to use the normal meaning character "." In a regular expression, you must use the escape character "\".
In regular expressions, you can enclose a number of characters in square brackets to represent a metacharacters, which used by itself represents any one of the characters in square brackets, such as "reg=" [abc]4 "", "string" A4 "," B4 "," C4 "
are all strings that match the regular expression. The square bracket metacharacters can also be in other formats, as follows:
// [^456]: represents any character other than 4, 5, 6. //[A-r]: represents any one of the a~r. //[a-za-z]: can represent any one English letter. //[A-e[g-z]]: represents a~e, or any one of the letters (and operations) in G~z. //[A-o&&[def]]: Represents D, E, F (intersection operation). //[A-D&&[^BC]]: Represents the letter A, D (difference operation).
A qualified modifier is allowed in a regular expression to limit the number of occurrences of metacharacters, such as "A *" for A can occur 0 or more times in a string. The usage of the Modifier qualifier is as follows:
Example of qualifying modifier Meanings
? 0 or 1 times A?
* 0 or more times a *
+ One or more +
{n} just appears n times a{2}
{N,} appears at least n times a{5,}
{N,m} Appears n~m Times a{3,8}
Example 6.1, create a class checkout, in the main method in the class to define whether the validation string is a legitimate mobile phone number method, the code is as follows:
ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classCheckOut { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString regex= "0\\d{2,3}[-]?\\d{7,8}|0\\d{2,3}\\s?\\d{7,8}" + "|13[0-9]\\d{8}|15[1089]\\d{8}";//Regular expression to verify the phone numberString str= "13412345678";//the phone number to be verifiedPattern pattern=pattern.compile (regex);//Compiling regular ExpressionsMatcher Matcher=pattern.matcher (str);//create a match for a given input pattern BooleanBool=matcher.matches (); if(bool==true) {//if validation is trueSYSTEM.OUT.PRINTLN ("Legal Regular expression");//give a hint}Else{//if validation is falseSYSTEM.OUT.PRINTLN ("Illegal regular expression")); } }}
The result of Instance 6.1 execution is: a valid regular expression.
Description: Phone number includes fixed phone and mobile phone number, where fixed phone is composed of area code and number, the area code starts with 0, followed by two or three digits, so when matching the area code, you can use
Regular expression "0\d{2,3}", fixed phone numbers are composed of 7~8 digits, so you can use the expression "\d{7,8}" to match. Because the combination of fixed phone may be "area code-number" or
"Area code number", so you can use the expression "0\\d{2,3}[-]?\\d{7,8}|0\\d{2,3}\\s?\\d{7,8}" to match a fixed phone number. The phone number is 11 digits and starts with the number "1", taking into account the mobile phone number
, the regular expression "13[0-9]\\d{8}|15[1089]\\d{8}" to match the mobile phone number.
Java String Collation notes (iv)