Regular expression Operator Precedence Introduction _ Regular Expression

Source: Internet
Author: User
Tags character set

Regular expressions are evaluated from left to right and follow the order of precedence, which is very similar to an arithmetic expression.

The same priority of the operation from left to right, the operation of different priorities first high and then low. The following table describes the order of precedence for the various regular expression operators, from highest to lowest:

operator Description
\ Escape character
(), (?:), (?=), [] Parentheses and square brackets
*, +,?, {n}, {n,}, {n,m} Qualifier
^, $, \ Any meta character, any character Anchor points and sequences (i.e., position and order)
| Replace, "or" action
The character has precedence over the substitution operator, making "M|food" match "M" or "food". To match "mood" or "food", create a subexpression using parentheses to produce a "(m|f) Ood."

Today's table is a subtotal of the metacharacters in the overall regular expression syntax. Help memorize the meta characters. Ditto, this document is reproduced to MSDN.

Operator

The following table describes the order of precedence for the various regular expression operators, from highest to lowest:

Operator

Description

\

Escape character

(), (?:), (?=), []

Parentheses and brackets

*, +,?, {n}, {n,}, {n,m}

Qualifier

^, $, \ Any meta character, any character

Anchor points and sequences

|

Replace

The character has precedence over the substitution operator, making "M|food" match "M" or "food". To match "mood" or "food", create a subexpression using parentheses to produce a "(m|f) Ood."

Special Character Tabulation

Special characters

Comments

$

Matches the position of the end of the input string. If the Multiline property of the RegExp object is set, then $ also matches the position of \ n or \ r before. To match the $ character itself, use \$.

( )

Marks the beginning and end of a subexpression. You can capture the subexpression for later use. To match these two characters, use \ (and \).

*

Matches the preceding character or subexpression 0 or more times. To match the * character, use \*.

+

Matches the preceding character or subexpression one or more times. to match the + character, use \+.

.

Matches any single character except \ n of a newline character. to match., please use \.

[ ]

Marks the beginning of an expression in parentheses. To match these characters, use \[and \].

?

Matches the preceding character or subexpression 0 times or once, or indicates a "not greedy" qualifier. to match? characters, please use \?.

\

Marks the next character as a special character, text, reverse reference, or octal escape character. For example, the character n matches the character N. \ n matches line breaks. Sequence \ match \, sequence \ (.

/

Represents the beginning or end of a text regular expression. to match/character, use \/.

^

Matches the position at the beginning of the input string, except where it is used in the bracket expression, in which case it is reversed against the character set. To match the ^ character itself, use \^.

{ }

The beginning of a tag qualifier expression. To match these characters, use \{and \}.

|

Indicates that a selection is made between two items. to match |, use \|.

Qualifier meaning

Character

Description

*

Matches the preceding character or subexpression 0 or more times. For example, zo* matches Z and Zoo. * is equivalent to {0,}.

+

Matches the preceding character or subexpression one or more times. For example, zo+ matches zo and zoo, but does not match Z. + is equivalent to {1,}.

?

Matches the preceding character or subexpression 0 times or once. For example, do (es)? Match do in do or does. is equivalent to {0,1}.

{n}

N is a non-negative integer. Matches n times exactly. For example, o{2} does not match the O in Bob, but matches two o in food.

{n,}

N is a non-negative integer. Match at least N times. For example, O{2,} does not match the O in Bob, but matches all o in the Foooood. O{1,} is equivalent to o+. O{0,} is equivalent to o*.

{n,m}

m and n are nonnegative integers, where n <= m. Match at least N times, at most m times. For example, o{1,3} matches the first three o in Fooooood. o{0,1} is equivalent to O?. Note: You cannot insert a space between commas and numbers.

Because the chapter number is likely to exceed nine in a large input document, you need a way to handle a two-bit or three-bit chapter number. Qualifiers give you this ability. The following regular expression matches a chapter title numbered to any number of digits:

/chapter [1-9][0-9]*/

Notice that the qualifier appears after the range expression. Therefore, it applies to the entire range expression, in this case, specifying only numbers from 0 to 9 (including 0 and 9).

The + qualifier is not used here because a number does not necessarily need to be in the second or subsequent position. Also not used? character, because it limits the chapter number to only two digits. You need to match at least one number following the Chapter and space characters.

If you know that the chapter number is limited to only 99 chapters, you can use the following expression to specify at least one but up to two digits.

/chapter [0-9]{1,2}/

The disadvantage of the above expression is that the chapter number greater than 99 still matches only the initial two digits. Another disadvantage is that Chapter 0 will also match. A better expression that matches only two digits is as follows:

/chapter [1-9][0-9]?/

Or

/chapter [1-9][0-9]{0,1}/

*, + and? Qualifiers are called "greedy" because they match as much text as possible. However, sometimes you only need a minimal match.

For example, you might search an HTML document to find chapter headings enclosed in H1 tags. This text is in your document as follows:

The following expression matches everything from the beginning less than the symbol (<) to the greater-than sign (>) that closes the H1 tag.

/<.*>/

If you only need to match the start H1 tag, the following "non-greedy" expression only matches <H1>.

/<.*?>/

Through in *, + or? Qualifier, the expression is converted from a greedy expression to a "not greedy" expression or a minimum match.

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.