Dark Horse programmer: Java Regular Expression verification email and mobile phone number

Source: Internet
Author: User

-------
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 ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.