Overview of regular expressions and their use in Java

Source: Internet
Author: User
Tags regular expression

Overview

Have you ever used to find *.tmp temporary files?

Have you ever used the Find/Replace feature in the editing tool?

In fact, they all use regular expressions

The role of regular expressions

Provides more powerful string processing capabilities

To test patterns within a string

For example, you can test the input string to see if the phone number pattern or the ID number pattern appears within the string. i.e. data validation

Replace text

You can use regular expressions to identify specific text in a document, to completely delete the text, or to replace it with other text.

Extract substrings from strings based on pattern matching

You can find specific text within a document or in an input field.

Using regular Expressions in Java

The regular expression has a powerful function in string processing, and sun has added support for it in jdk1.4

The Java.util.regex package is included in jdk1.4 to provide support for regular expressions. And the ReplaceAll and split functions in the Java.lang.String class are also implemented by regular expressions called.

The action of a regular expression on a string consists mainly of:

1. String Matching

2. Specify string substitution

3. Specify string Lookup

4. String segmentation

Pattern class

An instance of the pattern class represents a regular expression specified as a string, with a syntax similar to that used in Perl.

A regular expression that is specified as a string must first be compiled into an instance of the pattern class. The generated pattern is used to create the Matcher object, which matches any sequence of characters based on the regular expression. Multiple matches can share a pattern because it is not exclusive

The given regular expression is compiled into a pattern using the compile method, and then a match is created using the Matcher method, which matches the given input according to this pattern. The pattern method returns the regular expression used to compile the schema.

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.