"Cicada Hall Learning Notes" Java Regular expressions

Source: Internet
Author: User

This article refers to the network above the other people's Blog knowledge output

Regular Expression Basics

1. Period

Suppose you want to find three-letter words, and the words must start with a "T" letter and End with an "n" letter. Also, if you have an English dictionary, you can use regular expressions to search for all of its contents. To construct this regular expression, you can use a wildcard character-the period symbol "." Thus, the complete expression is "T.N", which matches "tan", "ten", "Tin" and "ton", also matches "t#n", "TPN" and even "T n", there are many other meaningless combinations. This is because the period symbol matches all characters, including spaces, tab characters, and even line breaks:

Regular expression: T.N

Matches: Tan,ten,tin,ton,t n,t#n, et

1.2 Square brackets Symbol

In order to solve the problem that the period symbol matching range is too wide, you can specify the characters that appear to be meaningful in square brackets ("[]"). At this point, only the characters specified inside the square brackets characters participate in the match. In other words, the regular expression "t[aeio]n" only matches "tan", "Ten", "Tin" and "ton". But "Toon" does not match, because within square brackets you can only match a single character:

Regular expression: T[aeiou]n

Matches: Tan,ten,tin,ton

1.3 "or" symbol

If you want to match "toon" in addition to all the words that match above, then you can use the "|" Operator. | The basic meaning of an operator is a "or" operation. to match "Toon", use the "t (A|e|i|o|oo) n" Regular expression. You cannot use a square extension, because the square brackets allow only a single character to be matched, and the parentheses "()" must be used here. Parentheses can also be used for grouping, see later.

Regular expression: T[a|e|i|o|u|oo]n

Matches: Tan,ten,tin,ton,toon

1.4 Symbol for number of matches

Table One shows the symbols that represent the number of matches, which are used to determine the number of occurrences of the symbol immediately to the left of the symbol:

Symbol representing the number of times
Symbol Number
* 0 or more times
+ 1 or more times
? 0 or one time
N Exactly n times
{N,m} From N times to M times

Suppose we want to search the text file for Social Security numbers in the United States. The format of this number is 999-99-9999. The regular expression used to match it is shown. In regular expressions, a hyphen ("-") has a special meaning, which represents a range, such as from 0 to 9. Therefore, when you match a hyphen symbol in a social security number, it is preceded by an escape character "/".

[0-9] {3} \-[0-9]{2} \-[0-9]{4}

So it's going to match all the social Security numbers.

If you're searching, you want a hyphen to appear or not-that is, 999-99-9999 and 999999999 are in the right format. At this point, you can add the word "?" after the hyphen symbol. The quantity-qualified symbol, two, shows:

[0-9] {3} \-? [0-9] {2} \-? [0-9] {4}

Match all 123-12-1234 and 123121234 forms of Social Security numbers

Let's look at another example here. One format for American car licences is four digits plus two letters. Its regular expression is preceded by the number part "[0-9]{4}", plus the letter part "[A-z]{2}". Figure three shows the complete regular expression

[0-9] {4} [a-z]{2}

Match a typical U.S. car license number, such as 8836KV

1.5 "no" symbol

The "^" symbol is called a "no" symbol. If used in square brackets, "^" indicates a character that you do not want to match. For example, the regular expression in Figure four matches all words, except for words that begin with an "X" letter.

[^x] [a-z]+

Matches all words, except for "X"

1.6 Parentheses and blank symbols

Suppose you want to extract the month portion from a birthday date formatted as "June 26, 1951," and the regular expression used to match that date can be shown in five:

[a-z]+ \s+[0-9]{2}, \s* [0-9]{4}

Dates matching all moth dd,yyyy formats

The newly-appearing "/S" symbol is a blank symbol that matches all whitespace characters, including the tab character. If the strings match correctly, you can simply create a group by adding a parenthesis around the month to extract the month part.

([a-z]+) \s+ [0-9]{2}, \s* [0-9]{4}

Matches all dates in month dd,yyyy format, defines the month value as the first group

1.7 Other symbols

For simplicity, you can use some shortcut symbols that are created for common regular expressions.

Common symbols
Symbol An equivalent regular expression
\d [0-9]
\d [^0-9]
\w [A-z0-9]
\w [^a-z0-9]
\s [\t\n\f]
\s [^\t\n\f]

For example, in the example of the previous social Security number, we can use "/d" where all occurrences of "[0-9]" occur. Modifications can be expressed as:

\D{3} \-\d{2} \-\d{4}

Matches all social Security numbers in 123-12-1234 format

http://www.zhiliaotang.com Please pay attention to Cicada Hall, there will be more learning partners together to progress together.

Cicada Hall Learning note Java Regular expression

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.