Regular expression matching rules

Source: Internet
Author: User
Basic Pattern Matching

Everything starts from the most basic. Patterns are the most basic elements of formal expressions, which are a set of characters that describe the character of a string. Patterns can be simple, consist of ordinary strings, or can be very complex, often with special characters representing a range of characters, repeating, or representing context. For example:

^once

This pattern contains a special character ^, which indicates that the pattern matches only those strings that begin with once. For example, the pattern matches the string "Once Upon a Time" and does not match "there once is a man from NewYork". Just as the ^ symbol represents the beginning, the $ symbol is used to match strings that end in a given pattern.

bucket$

This pattern matches the "who kept all of the cash in a bucket" and does not match "buckets". The characters ^ and $ are used together to indicate exact matches (the string is the same as the pattern). For example:

^bucket$

Matches only the string "bucket". If a pattern does not include ^ and $, then it matches any string that contains the pattern. Example: Mode

Once

With string

There once is a man from NewYork

Who kept all the cash in a bucket.

is a match.

The letters in the pattern (O-N-C-E) are literal characters, that is, they represent the letter itself, and the numbers are the same. Some other slightly more complex characters, such as punctuation and white characters (spaces, tabs, etc.), are used to escape sequences. All escape sequences begin with a backslash (\). The escape sequence for a tab is: \ t. So if we're going to check if a string starts with a tab, you can use this pattern:

^\t

Similarly, use \ n to indicate "new line" and \ r for carriage return. Other special symbols can be used in front with a backslash, such as the backslash itself with \ \, period. Use \. To indicate, and so on.

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.