Basic syntax for regular expressions

Source: Internet
Author: User
Tags range
Grammar | First let's look at two special symbols ' ^ ' and ' $ '. Their role is to point out the beginning and end of a string, respectively. Examples are as follows:

"^the": denotes all strings starting with "the" ("There", "the Cat", etc.);
"Of despair$": a string representing the end of "of despair";
"^abc$": means that the beginning and end are "abc" string-hehe, only "ABC" itself;
"Notice": Represents any string containing "notice".

Like the last example, if you don't use two special characters, you're saying that the string you want to find is in any part of the string you're looking for--you
Do not position it at a certain top.

and other ' * ', ' + ' and '? ' The three symbols that represent the number of occurrences of one or a sequence of characters. They represent "no or
More "," one or more "and" No or once ". Here are a few examples:

"ab*" means that a string has one followed by 0 or several B. ("A", "AB", "ABBB",...... );
"ab+" means that a string has a second followed by at least one B or more;
"Ab?" : Indicates that a string has a followed 0 or a B;
"a?b+$": indicates that there are 0 or one a followed by one or several B at the end of the string.

You can also use a range, enclosed in curly braces, to indicate the range of repeat times.

"Ab{2}": Indicates that a string has a followed 2 B ("ABB");
"Ab{2,}": Indicates that a string has a a followed by at least 2 B;
"ab{3,5}": Indicates that a string has a followed 3 to 5 B.

Please note that you must specify the lower bound of the range (for example: "{0,2}" instead of "{2}"). Also, you may have noticed, ' * ', ' + ' and
'?' Equivalent to "{0,}", "{1,}" and "{0,1}".
There is also a ' ¦ ', which indicates the "or" action:

"Hi¦hello": means "hi" or "Hello" in a string;
"(B¦CD) EF": means "bef" or "cdef";
"(a¦b) *c": Represents a string of "a" "B" mixed strings followed by a "C";

'.' You can override any character:

"A.[0-9]": Indicates that a string has a "a" followed by an arbitrary character and a number;
"^. {3}$ ": A string representing any three characters (3 characters in length);

The square brackets indicate that certain characters are allowed to 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 lowercase ' a ' to ' d ' (equivalent to "a¦b¦c¦d" or "[ABCD]");
"^[a-za-z]": Represents a string that begins with a letter;
"[0-9]%": Indicates a digit before a percent semicolon;
", [a-za-z0-9]$": Indicates that a string ends with a comma followed by a letter or number.

You can also use ' ^ ' in square brackets to indicate a character that you do not want to appear, ' ^ ' should be first in square brackets. (such as: "%[^a-za-z]%" table
The letter should not appear in the two percent sign.

In order to express it verbatim, you have to be in "^.$ () ¦*+?" {\ ' Precede these characters with the transfer character ' \ '.

Note that in square brackets, you do not need an escape character.





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.