Application of regular Expressions in Java (II.)

Source: Internet
Author: User
Tags regular expression

Brief introduction:

Java.util.regex is a class library package that uses patterns that are customized by regular expressions to match strings.

It consists of two classes: pattern and Matcher

Pattern A pattern is a compiled representation of a regular expression.
Matcher A Matcher object is a state machine that expands the matching check for strings based on pattern objects as match patterns.

First, a pattern instance is used to order a compiled schema of a similar regular expression in Perl, followed by a Matcher instance that matches the string under the mode control of the given pattern instance.

Let's take a look at these two classes separately:

Pattern class:

The pattern method is as follows:

static pattern compile (String regex)

Compiles and assigns the given regular expression to the pattern class

static Pattern compile (String regex, int flags)

Ditto, but increases the specified flag parameter, optional flag parameters include: case Insensitive,multiline,dotall,unicode case, CANON EQ

int flags ()

Returns the matching flag parameter for the current pattern.

matcher matcher (Charsequenc e input)

Generate a given named Matcher object

matches (String regex, charsequence input)

Compiles a given regular expression and matches the input string with the regular expression, which is appropriate for the case where the regular expression is used only once, that is, only one matching operation Because there is no need to generate a Matcher instance in this case.

pattern ()

Returns the PA The regular expression that the Tter object compiles.

string[] spli T (charsequence input)

Splits the target string according to the regular expression contained in the pattern.

string[] spli T (charsequence input, int limit)

Function above, add parameter limit the purpose is to specify the number of segments, such as the Limi set to 2, then the target string will be divided according to the regular expression cut to two paragraphs.

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.