Regular Expressions in Java

Source: Internet
Author: User

I. What is a regular expression

A regular expression is a template for matching strings, which matches a batch of strings, so creating a regular expression is creating a special string.

Common Regular Expression characters

Character

Explain

\ t

Tabs

\ n

Line break

\ r

Carriage return character

$

Matches the end of a line, and if you want to match the $ string, use \$ to not write

^

Matches the start of a line, and if you want to match the ^ string, use \^ to not write

()

Marks the start and end of the subexpression, if you want to match (or) use \ (or \)

[]

Used to determine the start and end positions of the bracket expression. [] determines the range of content that the range matches substrings

{}

The length match of the occurrence frequency substring used to mark the front face expression

*

Specifies that a front-face expression can occur 0 or more times

+

Specifies that a front-face expression can occur one or more times

?

Specifies that front-face expressions can occur 0 or more times

.

Matches any single character except for the newline character \ n.

\

Used to escape the next character.

|

Specifies an option between two items.

Character

Explain

\d

Match all numbers in 0-9

\d

Match non-numeric

\s

Matches all whitespace characters, including spaces, tabs, carriage returns, page breaks, line feeds

\s

Match all non-whitespace characters

\w

Match all the word characters, including all the numbers, 26 letters and 0~9 (_)

\w

Matches all non-word characters.

Square Expansion Number

Explain

Represents an enumeration

[ABC], which represents a, B, c any one of the characters, [GZ] flag g, z any one of the characters

Indication Range:-

[A-f], which represents any character between a and F, the range can be used in conjunction with an enumeration, such as: [A-fx-z] matches any character A to f or X to Z

Indicates no: ^

[^a-f], which represents any character of non-A, B, C, D, E, f All characters are excluded a-f ^ reject symbol

Representation and:&&

[A-z&&[d-f]], representing A to Z with the set of DF, D, E or F

[a-z&&[^bc]],a-z all characters except BC.]

[A-z&&[^d-h]],a-z all characters remove characters between d-h, that is, between A-c and I-z

Represents the "and" Operation

[A-dm-p], which is all the characters between a-d all characters and m-p

Braces

Explain

X?

X expression appears 0 or one time

x*

An X expression appears 0 or more times

x+

An X expression appears one or more times

X{n}

x expression appears n times a{5} AAAAA

X{n,}

X-expression appears at least n times

X{N,M}

X-expression appears n to M times

Two. Using regular expressions

1. Using regular expressions in your program, you can use the pattern and Matcher classes provided by Java to work with regular expressions.

The pattern object is an in-memory representation of a regular expression after it is compiled, so the regular expression string must first be compiled into a pattern object, and then the pattern object is used to create the corresponding Matcher object. The States involved in performing a match remain in the Matcher object, and multiple Matcher objects can share the same pattern object.

Style:

Pattern pattern = pattern.compile ("Established regular expression"= Pattern.matcher ("matched string");
System.out.println (M.matches ());
// returns a Boolean value

2. If a regular expression needs to be used only once, you can directly use the static matches method of the pattern class, which automatically compiles the specified string into an anonymous pattern object and performs a matching

Example 2:

boolean flag = pattern.matches ("Regular expression created", "string"
SYSTEM.OUT.PRINTLN (flag);

Regular Expressions in Java

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.