Collected Java Regular expressions

Source: Internet
Author: User

Regular expressions are a pretty good thing, allows you to parse a string using a specified pattern, which Perl did by providing a function similar to a regular expression later unrivaled martial arts, not beautiful (but now the mainstream programming language has provided regular expression function), but it seems to look more bizarre and complex, And to remember those operators seem more difficult, so from the Internet to collect and tidy up a bit, convenient for their own convenience you, yes! :

. (matches any single character.) For example, regular expression r.t match these strings: Rat, Rut, R T, but do not match root

$ (matches the line end character.) For example, the regular expression weasel$ can match the end of the string "he" a weasel, but cannot match the string "They are a bunch of weasels.". )

^ (matches the start of a row.) For example, regular expression ^when in can match the start of the string "When in the course of human events", but cannot match "What and" in ". )

* (match 0 or more of the characters just before it.) For example, regular expressions. * means to be able to match any number of any characters. )

\ (This is a reference character used to match the meta characters listed here as normal characters.) For example, the regular expression \$ is used to match the dollar sign, not the end of the line, similar to the regular expression \. Used to match the dot character, not any character's wildcard characters.

[] [C1-C2] [^C1-C2] (matches any one of the characters in parentheses.) For example, regular expression r[aou]t matches rats, rot, and rut, but does not match ret. You can use hyphens in parentheses to specify the range of characters, for example, regular expressions [0-9] can match any number of characters, and you can also develop multiple intervals, such as regular expression [a-za-z], which can match any uppercase and lowercase letters. Another important use is "exclusion", to match a character other than a specified interval--that is, a so-called complement--use the ^ character between the left parenthesis and the first character, for example, regular expression [^269a-z] matches any character except 2, 6, 9, and all uppercase letters. )

\< \> (Word) Start (\<) and End (\>). For example, the regular expression \<the can match the "the" in the string "for the Wise", but it cannot match the "the" in the string "otherwise". Note: This meta character is not supported by all software. )

\ (\) (defines the expression between \ (and \) as "group", the characters that match the expression are saved to a staging area (up to 9 in a regular expression), and they can be referenced using symbols. )

| (Two matching criteria are logically "or" (or) operations.) For example, regular expressions (him|her) match "it belongs to him" and "it belongs to her", but they cannot match "it belongs to them." Note: This meta character is not supported by all software. )

+ (matches 1 or more of the character just before it.) For example, regular expression 9+ matches 9, 99, 999, and so on. Note: This meta character is not supported by all software. )

? (Match 0 or 1 of the character just before it.) Note: This meta character is not supported by all software. )

\{i\} \{i,j\} (matches a specified number of characters that are defined in the expression that precedes it.) For example, the regular expression a[0-9]\{3\} can match a string of exactly 3 numeric characters followed by the character "A", such as A123, A348, but does not match A1234. The regular expression [0-9]\{4,6\} matches any 4, 5, or 6 consecutive numeric characters that are contiguous. Note: This meta character is not supported by all software. )

Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]

Match two-byte characters (including Chinese characters): [^\x00-\xff] (a double-byte character length meter 2,ascii character 1)

A regular expression that matches a blank row: \n\s*\r

Matching mobile phone number: ^ (1[358][0-9]{1}) [0-9]{8}$

Regular expression:< matching HTML tags (\s*?) [^>]*>.*?</\1>|<.*? /> (also can only match part)

A regular expression that matches the end-end whitespace character: ^\s*|\s*$

Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

Regular expressions that match URL URLs: [a-za-z]+://[^\s]*

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.