Java Regular Expressions

Source: Internet
Author: User

1. What is a Java regular expression

1, RegularExpressions (Regex) A regular expression is a pattern used to describe a certain amount of text, often used for: string matching, finding, and replacing.
2, the regular expression is a weapon of string processing, it is a description of the string pattern of the formula, the core value of a regular expression is to match a string.

3. Example:

        //  define matching rules to import java.util.regex.* of regular expression packages;        Pattern p=pattern.compile ("\\d");         // give the package import Java.util.regex.Matcher to match something that needs to be imported into the regular expression;         Matcher M=p.matcher ("1");             // The matches () method gives the match result-match: True, mismatch: false         System.out.println (M.matches ());    

2. Rules for regular expressions

2.1 Common Regular Expressions

2.2 Quantity representation

2.3 Logical operators

3, Case reference

3.1 e-mail: ^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$ (standard) reference ^\[email protected]\w+\.\w+$

3.2 China Phone number: ^ (\ (\d{3}\) |\d{3}-)? \d{8} (Standard) reference ^0[0-9]{2,3}-[1-9][0-9]{6,7}$

3.3 Verify that the address of the internal Tomcat server meets the requirements

        Pattern p = pattern.compile ("http:/(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d{1,5})/\\w+\\.\\w+");         = P.matcher ("http://127.0.0.1:8081/index.html");         if (M.find ())        {            System.out.println ("The server's address is:" +m.group (1));            System.out.println ("The port is:" +m.group (2));        }
Output Result:

The address of this server is: 127.0.0.1
The port is: 8081

Java Regular Expressions

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.