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