Java Regular Expressions The simplest learning tutorial

Source: Internet
Author: User

This tutorial with a verification of mobile phone number expression as an example, after learning This example, basically all the expressions can be understood, but if you want to get started to write the degree, but also need to practice more testing Ah!

To see such a code

" ^ ((13[0-9)) | ( 15[^4,\\D]) | (18[0,0-9])) \\d{8}$"; if (! mobilephone.matches (Mobilephonereg)) {Showtoast (" phone number format is incorrect! ) " ); return false ;}

1, string "13"

From the very beginning of the 13来, if the string of the regular expression is "13", it means matching the string "13", if not "13" is definitely the result of matches false.

2, English period "."

“.” Meaning, can be understood as a placeholder, "1.3" means to match three digits, the beginning and end must be 1 and 3, such as "143", "1..3" identity matches four digits, for example "143" now match the words will not match.

3. Square brackets "[]"

"[]" can be considered as "." Extension, is a placeholder, the difference is only "[]" the element is further qualified, such as "1[fb4]3" means only match "143", "1f3", "1b3" the three, if anything else, all will not accept.

4, parentheses "()"

"()" Compared to "[]" and expanded, "[]" is just a placeholder, "()" can represent more than one placeholder, such as "1 (ABC) 3" means only match "1ABC3", of course you can also use "|" To support the effect of group matching, for example, "1 (abc|2) 3" means matching only "1ABC3" or "123".

5, "|"

In parentheses, it's actually mentioned, "|" Is like what he means in the code: or.

6, "-"

"-" in the regular expression may appear in two scenarios, a separate "-", One is "\-", the meaning of the two occasions is also different, the former represents the interval concept, such as "1[1-3]3", indicating matching "113", "123", "133", Outside of the parentheses, of course, the intent is a purely symbolic--that is, the escape character "\" is used, for example "1[\-]3" means only the match character "1-3"

7, the number of matching times series

It is worth noting that the symbols that represent the series of matches are all valid for their previous character, such as "13*" for matching "1 ...", "13+" for matching "13 ....", "13?" means match "1" or "13", "13{2}" means match "133", "13{2,3}" means match "133" or "1333".

8, extermination division too "^"

"^" denotes a negative meaning and must be used in parentheses, in front of whom it means that no one is allowed to appear. For example, "[^1]3" means that the second bit is 3, except for the beginning of 1.

9, "/S"

"/S" means a space, such as "/s+", that matches at least one space

Java Regular Expressions The simplest learning tutorial

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.