Java Basics-Regular expressions

Source: Internet
Author: User

Simple discussion of the syntax rules for regular expressions:

1. Ordinary characters

Letters, numbers, Chinese characters, underscores, and punctuation marks that are not defined as special meanings are "ordinary characters".

Description: "Normal character" matches one of the same characters when matched.

If all the characters in an expression are "normal characters", then the matching operation of this expression is actually a normal "string lookup" operation.

2. Simple escape character "\"

  \ n: line break = \x0a

  \ r: return character = \x0d

  \ t: Tab = \x09

Some are defined as special meanings, so you need to add "\" in front to match the punctuation of the character itself:

^ $ ( ) [ ] { } . ? + * |

Example: to match the first column "^" character itself, use "\^", other similar

3. Custom Character Set []

Custom Character classes Representative meaning
[ABC] a,b or C(Simple Class)
[^ABC] Any character except a,b , or C(negation)
[A-za-z] a to z or a to z, the letters at both ends are included (range)
[A-d[m-p]] a to D or m to p:[a-dm-p](set)
[A-z&&[def]] d,e or F(intersection)
[A-Z&&[^BC]] a to Z, except for b and C:[ad-z](minus)
[A-z&&[^m-p]] a to z, not m to p:[a-lq-z](minus)

Description

A regular expression that can match any one of the characters in "multiple characters". Although it is "multiple characters", it can only match one at a time.

4. Predefined character classes

Predefined character classes

Representative meaning

.

Any character (may or may not match the ending character)

\d Numbers:[0-9]
\d Non-numeric: [^0-9]
\s whitespace characters:[\t\n\x0b\f\r]
\s Non-whitespace characters:[^\s]
\w Word character:[a-za-z_0-9]
\w Non-word characters:[^\w]

5. Boundary Matching Device

Boundary Matching Device Representative meaning
^ The beginning of the line
$ End of Line
\b Word boundaries

Description

An expression that itself does not match any characters, only attaches a condition to the character boundary and the gap between the characters.

6. Match Number Qualifier

Description

The modified expression can be repeated in a fixed number of times, or a certain number of repeated matches can be qualified.

7. Grouping ()

Enclose other expressions in parentheses () to make the contained expression a whole, which can be modified as a whole when the number of matches is modified.

In addition, the parentheses contain the expression, the match to the content will be recorded separately, in the matching process or after the end can be obtained.

Description

Each pair of parentheses is assigned a number, and the capture using () is automatically numbered starting with 1, based on the order of the opening brackets. Capturing the first catch with element number zero is the text that is matched by the entire regular expression pattern.

Java Basics-Regular expressions

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.