-------
Android Training , Java Training We look forward to communicating with you!
----------
Regular Expression:It is actually a rule used to operate strings.
Advantage: Regular Expressions make it easier to perform complex operations on strings.
Feature:CodeIt is represented by some symbols. With the specified symbol, you can call the underlying code to operate the string. The appearance of symbols simplifies code writing.
Disadvantages: the appearance of symbols simplifies writing, but reduces readability.
In fact, more problems are solved by using regular expressions.
GROUP: it is marked with parentheses. Each parentheses is a group with an automatic number starting from 1.
If you use a group, the corresponding number is the content of the group. Do not forget to add \ To the array \\.
(Aaa (wwww (CCC) (eee) tips, starting from the left parenthesis. There are several left parentheses.
Common Operations:
1. Matching: Actually, the matches method in the string class is used.
String Reg = "[1-9] [0-9] {4, 14 }";
Boolean B = QQ.Matches(REG); // associate the regular expression with the string to match the string.
2. Cut: The split method in the string class is used.
3. Replace: replaceall () in the string class ();
4. obtain:
1), first compile the regular expression into a regular object. The static method compile (RegEx) in pattern is used );
2) Get the matcher object through the pattern object.
Pattern is used to describe a regular expression and can be parsed.
The rule operation string needs to be re-encapsulated into the matcher object.
Then, use the matcher object method to operate the string.
How can I obtain a matcher object?
Use the matcher method in the pattern object. This method can be associated with strings based on regular rules. And returns the matching object.
3) use the method in the matcher object to perform various regular operations on the string.
The following is a Java example of a regular expression:
Import Java. util. regEx. matcher; import Java. util. regEx. pattern; public class checkmobileandemail {/*** verify that the email address is correct * @ Param email * @ return */public static Boolean checkemail (string email) {Boolean flag = false; try {string check = "^ ([a-z0-9A-Z] + [-| \.]?) + [A-z0-9A-Z] @ ([a-z0-9A-Z] + (-[a-z0-9A-Z] + )? \\.) + [A-Za-Z] {2,} $ "; pattern RegEx = pattern. compile (check); matcher = RegEx. matcher (email); flag = matcher. matches ();} catch (exception e) {flag = false;} return flag ;} /*** verify the mobile phone number ** @ Param mobiles * @ return [0-9] {5, 9} */public static Boolean ismobileno (string mobiles) {Boolean flag = false; try {pattern P = pattern. compile ("^ (13 [0-9]) | (15 [^ 4, \ D]) | (18 [-9]) \ D {8} $ "); matcher M = P. matcher (mobiles); flag = m. matches () ;}catch (exception e) {flag = false;} return flag;} public static Boolean isnum (string number) {Boolean flag = false; try {pattern P = pattern. compile ("^ [0-9] {5} $"); matcher M = P. matcher (number); flag = m. matches () ;}catch (exception e) {flag = false ;}return flag ;}}