Java Regular Expressions
The regular expression defines the pattern of the string.
Regular expressions can be used to search, edit, or manipulate text.
Regular expressions are not limited to a single language, but have subtle differences in each language.
Regular expression Instances
A string is actually a simple regular expression, such as the Hello world Regular expression that matches the "Hello World" string.
. (dot) is also a regular expression that matches any one of the words such as: "A" or "1".
The following table lists examples and descriptions of some regular expressions:
Regular Expressions |
Description |
This is text |
Match string "This is text" |
This\s+is\s+text |
Note the \s+ in the string . The \s+ after matching the word "This" can match multiple spaces, then match the is string, then \s+ match multiple spaces and then follow the text string. Can match this instance: this is text |
^\d+ (\.\d+)? |
^ defined with what to start with \d+ match one or more numbers ? Setting the options in parentheses is optional \. Match "." Instances that can be matched: "5", "1.5", and "2.21". |
Java Regular expressions are the most similar to Perl.
The Java.util.regex package mainly consists of the following three classes:
The
- pattern class:
Pattern object is a compiled representation of a regular expression. The Pattern class has no public constructor method. To create a pattern object, you must first call its public static compilation method, which returns a Pattern object. The method takes a regular expression as its first argument.
- Matcher class: The
Matcher object is the engine that interprets and matches the input string. Like the pattern class, Matcher does not have a public construction method. You need to invoke the Matcher method of the Pattern object to get a Matcher object.
- patternsyntaxexception:
Patternsyntaxexception is a non-mandatory exception class that represents a syntax error in a regular expression pattern.
/** * Get Current HttpSession * @author: Shijing * December 5, 2016 PM 3:46:02 * @return * * public static HttpSession getsess Ion () {return getrequest (). GetSession (); /** * Mobile phone number verification * @author: Shijing * December 5, 2016 PM 4:34:46 * @param str * @return Verified by return true */public static Boolean ismobile (Final String str) {Pattern p = null; Matcher m = null; Boolean B = false; p = pattern.compile ("^[1][3,4,5,7,8][0-9]{9}$"); Verify the phone number m = P.matcher (str); b = m.matches (); return b; */** * Phone number verification * @author: Shijing * December 5, 2016 PM 4:34:21 * @param str * @return Verified by return true */public static Boolean isphone (Final String str) {Pattern P1 = null, p2 = null; Matcher m = null; Boolean B = false; P1 = Pattern.compile ("^[0][1-9]{2,3}-[0-9]{5,10}$"); Verify that the P2 with area code = pattern.compile ("^[1-9]{1}[0-9]{5,8}$"); Verify that there is no area code if (Str.length () > 9) {m = P1.matcher (str); b = m.matches (); } else {m = P2.matcher (str); b = m.matches (); } return B; } public static void Main (string[] args) {String phone = "13900442200"; String phone2 = "021-88889999"; String phone3 = "88889999"; String phone4 = "1111111111"; Test 1 if (Isphone (phone) | | ismobile (phone)) {System.out.println ("1 This is compliant"); }//Test 2 if (Isphone (phone2) | | ismobile (PHONE2)) {System.out.println ("2 This is compliant"); }//Test 3 if (Isphone (phone3) | | ismobile (PHONE3)) {System.out.println ("3 This is compliant"); }//Test 4 if (Isphone (phone4) | | ismobile (phone4)) {System.out.println ("4 This is compliant"); }else{System.out.println ("non-conforming"); } }
If in doubt, please pay attention to the public number "amateur grass"!
Amateur grass Java regular expressions, verifying phone numbers and phone numbers