Regular expression assertions, patrol (assertions), forward assertions, reverse assertions _ Regular expressions

Source: Internet
Author: User
Tags assert

Assertions (assertions) are difficult to understand in regular expression concepts, which usually refers to a test performed at the current match of the target string, but this test does not occupy the target string, nor does it move the current matching position of the mode in the target string. In detail, we can look at the analysis of regular expression matching parsing (regular expression matching principle), which mentions "0 width" many metacharacters, but matches the special position, they can be understood as assertions.

Assertion Meta character

Common assertions of metacharacters are: \b, \b, \a, \z, \z, ^, $ they only represent special positions, each acting as if there is a string ab, with position representation as: 0a1b2

The
Meta character meaning (described above with position string)
^
$ line end, string tail, indicating position 2
\b
\b the
\a the
\z
\z
a,z,z,g rarely used

The tests for these assertions are based on the current location of the test, and the assertion also supports more complex test conditions. The more complex assertions are represented in a child mode, which includes the antecedent (forward) assertion (lookahead assertions) and the subsequent (Lookbehind assertions) assertion that the assertion only makes a matching judgment condition and is not recorded in the matching result. Characters are not matched.

Advance assertion, forward assertion, forward patrol (lookahead assertions)

The first assertion, often represented (? =pattern), starts with the current match to determine whether the following matching strings are set up, and (?!). pattern) in such two formats, let's take a look at an example. SOURCE string: "abc100", the regular expression is:
/[a-z]+ (? =\d+)/, we analyze the following process diagram:

First, the regular expression character [a-z]+ gets control, matches the character: "abc", the position matches from "0", becomes 3. Test whether the/d+ is valid from this location. Match to character 100, back to set. Therefore, the regular expression positive assertion succeeds. Returns the matching string "abc"

(?! pattern) Just, forward matching, when no match succeeds, will return true. The following is the system source string: abc100, the test results are as follows:

Then assert, reverse assert, reverse patrol (lookbehind assertions)

The following assertions are common expressions: (? <=pattern) or (? <!pattern) format. In regular expressions, do not appear in the fixed-length quantifiers, there may be a dead loop. Match error. Indicates that a matching character appears to the left of the current position, then returns true and the following match is normal. Because if it appears on the leftmost side, the default position starts at 0, and the match fails. Typically, you start a match from the following regular expression, and then backtrack until you match it. Let's look at the following example: source string: "abc100+=", the regular expression is: "(? <=\w) \w+", the matching procedure is as follows:

First, the regular expression character/\w+/gets control, matches the character: "abc100", the position matches from "0" and matches to 6 characters. The left variable \w match failed to be detected from this location 0. So/\w+/matches from character B to "bc100", tests the character "a" on the left side of it, and the reverse assertion is correct. So match to the string "bc100", (? <!pattern), just don't match the successful return true, the others are the same!

PostScript: From this article, we find that the search features are from left to there, generally forward to the assertion, after the regular expression, the reverse assertion is placed before the matching regular expression. However, this can also be put to the front or back. Here is no longer an example. Welcome to the Exchange discussion!

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.