Regular expression syntax

Source: Internet
Author: User

The regular expression function is too powerful ~ The following is an introduction to the basic syntax of Regular Expressions:

First, let's look at two special symbols '^' and '$ '. They indicate the start and end of a string respectively. Example:

"^ The": indicates all strings starting with "The" ("There", "The cat", etc );
"Of despair $": indicates the string ending with "of despair;
"^ Abc $": indicates that the start and end of the string are "abc"-Haha, only "abc" itself;
"Notice": indicates any string containing "notice.

As in the last example, if you do not use two special characters, you are indicating that the string to be searched is in any part of the searched string-you do not position it at a certain top.

Others include '*', '+', and '? 'These three symbols indicate the number of repeated occurrences of one or more characters. They indicate "none or more", "one or more", and "none or one ". The following are examples:

"AB *": indicates that a string has one a followed by zero or several B. ("A", "AB", "abbb ",......); "AB +": indicates that a string is followed by at least one B or more. "AB? ": Indicates that a string has one a followed by zero or one B."? B + $ ": indicates that there are zero or one a followed by one or several B at the end of the string.

You can also use a range enclosed in braces to indicate the range of repeated times.

"AB {2}": indicates that a string has a followed by two B ("abb ");
"AB {2,}": indicates that a string contains at least two B strings;
"AB {3, 5}": indicates that a string has 3 to 5 B following.

Note that you must specify the lower limit of the range (for example, "{0, 2}" instead of "{, 2 }"). Also, you may have noticed that '*', '+' and '? 'Is equivalent to "{0,}", "{1,}", and "{0, 1 }". There is also a '|', indicating "or" Operation:

"Hi | hello": indicates that a string contains "hi" or "hello ";
"(B | cd) ef": "bef" or "cdef ";
"(A | B) * c": represents a string of "a" "B" mixed strings followed by a "c ";

'.' Can replace any character:

"A. [0-9]": indicates that a string has a "a" followed by an arbitrary character and a number;
"^. {3 }$": represents a string of any three characters (Length: 3 characters );

Square brackets indicate that certain characters can appear at a specific position in a string:

"[AB]": indicates that a string has a "a" or "B" (equivalent to "a | B ");
"[A-d]": indicates that a string contains one of the lower-case 'A' to 'D' (equivalent to "a | B | c | d" or "[abcd]");
"^ [A-zA-Z]": a string that starts with a letter;
"[0-9] %": indicates a digit before the percent sign;
", [A-zA-Z0-9] $": represents a string ending with a comma followed by a letter or number.

You can also use '^' in square brackets to indicate unwanted characters. '^' should be the first character in square brackets. (For example, "% [^ a-zA-Z] %" indicates that no letter should appear in two percentage signs ).

For a verbatim expression, you must go to "^. $ () | * +? {"Add the Escape Character'' before these characters ''.

Note that escape characters are not required in square brackets.

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.