From: http://blog.csdn.net/xyydyyqf/article/details/9153313
During programming or computer usage, you often need to match, search, replace, and Judge strings. If you simply use the Code if (), whlie or something is complicated and troublesome. Regular Expressions are powerful and flexible text processing tools that specifically match, search, replace, and Judge strings.
Regular Expression matching characters
[Java]
View plaincopy
- // Match characters of Regular Expressions
- ^ Match from the beginning of the row
- $ Match to the end position of the row
- \ B matches the start or end position of a word
- . Match All arbitrary characters except line breaks
- \ W match word (a-Z, A-Z, 0-9, underline)
- \ W match non-single character
- \ S matches blank characters
- \ S match non-blank characters
- \ D match numeric characters (0-9)
- \ D match non-numeric characters
- * Matches the previous expression zero or multiple times, equivalent to {0 ,}
- + Match the previous expression once or multiple times, equivalent to {1 ,}
- ? Matches the previous expression zero or once, equivalent to {0, 1}
- {N} matches the previous expression n times. N is a non-negative integer.
- {N,} matches the previous expression at least N times. N is a non-negative integer.
- {N, m} matches the previous expression N-m times, and the NM values are non-negative integers.
- [] Character class. For example, [123] indicates any number in 123, and [1-9] indicates any number in 1-9.
- () Grouping, matching the whole in parentheses, (Java) + is matching a string that appears at least once in "Java"
- ^ Assense match, used before condition determination. For example,. * [^ \ D] \. Java $ is the last non-numeric Java file.
- | Logic or
- \ Escape characters, which are the same as those in programming languages
After learning about these matching characters, you can start to write the matching type. When many websites submit information, it is said that the information filled in is invalid because the regular expression is used to judge.
Mobile phone number verification: domestic mobile phones are all 11-digit numbers starting with 13, 15, and 18, which are used for matching verification.
Matching formula: \ B1 [358] \ D {9} \ BStart and end with \ B. The number starts with 1, and the second digit is a number in 3, 5, and 8. The remaining nine digits are numbers.
[Java]
View plaincopy
- Boolean check = false;
- String RegEx = "\ B1 [0, 358] \ D {9} \ B ";
- String input = "19100390888 ";
- Pattern pattern = pattern. Compile (RegEx) // compile the Regular Expression
- Matcher = pattern. matcher (input) // match
- If (matcher. Matches () Check = true;
- Return check;
ID card verification: the first six locations (numbers), the middle eight birthdays (numbers), and the last four identifiers (numbers ). The birthdate starts with 19 or 2, 01-12, and 01-31.
Matching formula: ^ \ D {6} (19) | (2 \ D) \ D {2} (0 \ D) | (1 [012]) ([0-2] \ D) | (3 [01]) \ D {4}) $
[Java]
View plaincopy
- Boolean check = false;
- String RegEx = "^ \ D {6} (19) | (2 \ D) \ D {2} (0 \ D) | (1 [012]) ([0-2] \ D) | (3 [01]) \ D {4}) $ ";
- String input = "19100390888 ";
- Pattern pattern = pattern. Compile (RegEx) // compile the Regular Expression
- Matcher = pattern. matcher (input) // match
- If (matcher. Matches () Check = true;
- Return check;
Email verification: Any digit in front of a non-null character must contain @, followed by. com or. cn
Matching formula: \ W + @ \ W + \. (COM) | (CN)
[Java]
View plaincopy
- Boolean check = false;
- String RegEx = "\ W + @ \ W + \. (COM) | (CN )";
- String input = "19100390888 ";
- Pattern pattern = pattern. Compile (RegEx) // compile the Regular Expression
- Matcher = pattern. matcher (input) // match
- If (matcher. Matches () Check = true;
- Return check;
/*
* *****
* Http://blog.csdn.net/xyydyyqf
* ***** 2013.6.23
*/