Regular expression Basic syntax

Source: Internet
Author: User

<--Basic Grammar:-->

①, two special symbols ' ^ ' and ' $ ', their role is to indicate the beginning and end of a string, respectively. For example:

"^the": denotes all strings startingwith "the" ("There","The Cat", etc.);
"Of despair$": Denotes A string that ends with " of Despair";
"^abc$": The start and end are "abc" string -- hehe, only "abc" himself;
"Notice": Represents any string that contains "notice" .

Note: If you do not use two special characters, it means that the string you are looking for is in any part of the found string -- and does not position it at a certain top.

②, There are other symbols: ' * ', ' + ' and '? ', indicating the number of occurrences of one or a sequence of characters. They mean "no or more", "one or more" and "no or one more". For example:

"Ab*": Indicates that a string has an a followed by 0 or several b. ("A", "AB", "ABBB",...... );

"Ab+": Indicates that a string has an a followed by at least one b or more;

"Ab?" : Indicates that a string has an a followed by 0 or a b;

"a?b+$": Indicates that there are 0 or a followed by one or several B at the end of the string.

You can also use ranges, enclosed in curly braces, to represent the range of repetitions.

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

Note: The lower bound of the range must be specified (for example: "{0,2}" instead of "{, 2}"). And, ' * ', ' + ' and '? ' Equals ' {0,} ', ' {1,} ' and ' {0,1} '.

There is also a ' | ', which means "or" operation;

"Hi" | "Hello": Indicates that there is "hi" or "Hello" in a string;

"(B|CD) EF": denotes "bef" or "cdef";

"(a|b) *c": Represents a String "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}$ ": A string representing any three characters (3 characters in length);

Square brackets indicate that certain characters allow a particular position in a string to appear:

"[AB]": Indicates that a string has a "a" or "B"(equivalent to "a¦b");
"[A-d]": Indicates a string containing a lowercase ' a ' to ' d ' one (equivalent to "a¦b¦c¦d" or "[ABCD]");
"^[a-za-z]": Represents a string that begins with a letter;
"[0-9]%": A number that represents a percent before a 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 unwanted characters, ' ^ ' should be the first in square brackets. (For example: "%[^a-za-z]%" means that no letters should appear in two percent signs).

In order to express it verbatim, you must be in "^." $ ( ) | * + ? {\ ' These characters are preceded by the transfer character ' \ '.

Note that in square brackets, you do not need to escape characters.

Regular expression Basic syntax

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.