Regular Expression overview and Application of Regular Expression in Java

Source: Internet
Author: User

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
  1. // Match characters of Regular Expressions
  2. ^ Match from the beginning of the row
  3. $ Match to the end position of the row
  4. \ B matches the start or end position of a word
  5. . Match All arbitrary characters except line breaks
  6. \ W match word (a-Z, A-Z, 0-9, underline)
  7. \ W match non-single character
  8. \ S matches blank characters
  9. \ S match non-blank characters
  10. \ D match numeric characters (0-9)
  11. \ D match non-numeric characters
  12. * Matches the previous expression zero or multiple times, equivalent to {0 ,}
  13. + Match the previous expression once or multiple times, equivalent to {1 ,}
  14. ? Matches the previous expression zero or once, equivalent to {0, 1}
  15. {N} matches the previous expression n times. N is a non-negative integer.
  16. {N,} matches the previous expression at least N times. N is a non-negative integer.
  17. {N, m} matches the previous expression N-m times, and the NM values are non-negative integers.
  18. [] Character class. For example, [123] indicates any number in 123, and [1-9] indicates any number in 1-9.
  19. () Grouping, matching the whole in parentheses, (Java) + is matching a string that appears at least once in "Java"
  20. ^ Assense match, used before condition determination. For example,. * [^ \ D] \. Java $ is the last non-numeric Java file.
  21. | Logic or
  22. \ 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
  1. Boolean check = false;
  2. String RegEx = "\ B1 [0, 358] \ D {9} \ B ";
  3. String input = "19100390888 ";
  4. Pattern pattern = pattern. Compile (RegEx) // compile the Regular Expression
  5. Matcher = pattern. matcher (input) // match
  6. If (matcher. Matches () Check = true;
  7. 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
  1. Boolean check = false;
  2. String RegEx = "^ \ D {6} (19) | (2 \ D) \ D {2} (0 \ D) | (1 [012]) ([0-2] \ D) | (3 [01]) \ D {4}) $ ";
  3. String input = "19100390888 ";
  4. Pattern pattern = pattern. Compile (RegEx) // compile the Regular Expression
  5. Matcher = pattern. matcher (input) // match
  6. If (matcher. Matches () Check = true;
  7. 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
  1. Boolean check = false;
  2. String RegEx = "\ W + @ \ W + \. (COM) | (CN )";
  3. String input = "19100390888 ";
  4. Pattern pattern = pattern. Compile (RegEx) // compile the Regular Expression
  5. Matcher = pattern. matcher (input) // match
  6. If (matcher. Matches () Check = true;
  7. Return check;


/*

* *****

* Http://blog.csdn.net/xyydyyqf

* ***** 2013.6.23

*/

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.