Regular Expressions (1)

Source: Internet
Author: User

I. Recognition of regular expressions

Regular Expressions, (English: Regular expression, often abbreviated in code as regex, RegExp, or re), a string-processing weapon, a concept of computer science. A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern .

1. Use

Regular expressions can be used for string matching (character matching), string lookups, string substitution, application specific occasions such as IP address checking, pulling out email addresses from web pages, and pulling links from web pages.

2. Java class
    • Java.lang.Srtring
    • Java.util.regex.Pattern
    • Java.util.regex.Matcher

  The Java.util.regex package, which is used to match a character sequence with a regular expression that specifies the pattern of the class.

   The pattern class, the compiled representation of a regular expression, can be simply understood as a pattern in which a string is to be matched.

  The Matcher class, a match, performs a matching operation on the character sequence by explaining the Pattern to a simple understanding of a result that results from a match.

A regular expression that is specified as a string must first be compiled into an instance of this class. The resulting pattern can then be used to create Matcher an object that, according to the regular expression, can match any sequence of characters . All the states involved in performing a match reside in the match , so multiple matches can share the same pattern .

The typical invocation order is:

Pattern p = pattern.compile ("A*b");
Matcher m = P.matcher ("Aaaab");
Boolean B = m.matches ();

When you use only one regular expression, you can easily pass the matches method defined by this class. This method compiles an expression and matches the input sequence in a single call. Statement

Boolean B = pattern.matches ("A*b", "Aaaab");

Or use the matches method of the String class

Boolean B = "Aaaab". Matches ("A*b");

They are all equivalent to the three statements above, although it is inefficient for duplicate matches because it does not allow the reuse of compiled schemas.

Common methods for Pattern classes:

Static // compiles the given regular expression into the pattern       // create a match for the given input with this pattern Static The boolean matches (String regex,  // compiles the given regular expression and attempts to match the given input with it.        // returns a regular expression in which this pattern has been compiled. 
3. Matching device

Creating a match from matcher a pattern by invoking the pattern's method, you can use it to perform three different matching operations after creating the match:

    • The matches method attempts to match the entire input sequence to the pattern.
    • The Lookingat method attempts to match the input sequence to the pattern from the beginning.
    • The find method scans the input sequence to find the next sub-sequence that matches the pattern.

Each method returns a Boolean value that represents success or failure, and more information about a successful match can be obtained by querying the status of the match.

Regular Expressions (1)

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.