Basic syntax of regular expression

zhaozj2021-02-08  298

Let us first look at the two special symbols '^' and '$'. Their role is to point out the beginning and end of a string. The example is as follows:

"^ 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 are string of "abc" - huh, only "ABC";

"Notice": indicates any string containing "Notice".

The last example, if you don't use two special characters, you are in any part of the string you want to find - you and

Do not position it on a top.

Others have the three symbols of '*', ' ' and '' ', indicating that one or a sequence character is repeated. They represent "no or

More "," once or more "and" no or once ". Below is a few examples:

"ab *": means a string having a A back followed by zero or several b. ("A", "ab", "abbb", ...);

"ab ": means that a string has a A back follows at least one B or more;

"ab?": Indicates a string having a A back followed by zero or one B;

"a? b $": indicates that there is zero or one A follow one or more b at the end of the string.

You can also use the scope, enclose with braces to indicate the range of repetitions.

"AB {2}": means a string has a A follow 2 B ("ABB");

"AB {2,}": means a string having a A follow at least 2 b;

"AB {3, 5}": indicates a string having a A followed 3 to 5 B.

Note that you must specify the lower limit of the range (such as "{0, 2}" instead of "{, 2}"). Also, you may notice, '*', ' ' and

'?' Is equivalent to "{0,}", "{1,}" and "{0, 1}".

There is also a '|', indicating "or" operation:

"hi & brvbarhello": indicates that there is "hi" or "hello" in a string;

"(B & Brvbarcd) EF": means "bef" or "cdef";

"(A & Brvbarb) * C": Represents a string of "a" "b" mixed strings and one "c";

'.' Can replace any character:

"a. [0-9]": indicates a string having a "A" behind follows an arbitrary character and a number;

"^. {3} $": Indicates a string (length of 3 characters); square brackets indicate that some characters are allowed to appear in a particular location in a string:

"[ab]": indicates a string having a "a" or "b" (equivalent to "a & brvbarb");

"[A-D]": means a string contains a lower-write 'a' into 'd' (equivalent to "A & Brvbarb & Brvbarc & Brvbard" or "[ABCD]");

"^ [A-ZA-Z]": indicates a string that starts with letters;

"[0-9]%": indicates a number before a percent sign;

", [A-ZA-Z0-9] $": Indicates that a string is followed by a comma with a letter or numbers.

You can also use '^' in square brackets to indicate the characters that do not want to appear, '^' should in square brackets. (Such as "% [^ a-za-z]%" table

Letters should not appear in two percent signs).

In order to express verbatics, you must add transfer characters '/' before "^. $ () | * ? {/".

Please note that in square brackets, no escape characters need.

转载请注明原文地址:https://www.9cbs.com/read-1336.html

New Post(0)