Introduction to regular expression Assertions, Assertions, forward Assertions, reverse Assertions, and regular expression Assertions

Source: Internet
Author: User

Introduction to regular expression Assertions, Assertions, forward Assertions, reverse Assertions, and regular expression Assertions

Assertions is hard to understand in the regular expression concept. It usually refers to a test conducted at the current matching position of the target string, but this test does not occupy the target string, that is, the current matching position of the mode in the target string is not moved. For details, see the regular expression matching parsing process for analysis (Regular Expression matching principle), which mentions that "Zero Width" is a multi-character, only matching for special locations, they can be understood as assertions.

Asserted metacharacters

Common asserted metacharacters include: \ B, \ B, \ A, \ Z, \ z, ^, and $. They only represent special positions. Their Respective functions include string AB, with location: 0A1B2

Metacharacters Meaning (description of the above position string)
^ The first line. The first string indicates the position 0.
$ End of line, end of string, indicating position 2
\ B Word dividing line, which can be expressed as 0, 2
\ B Non-word dividing line, which can represent 1 position
\ The start of the target (independent from the multiline mode) indicates the position 0.
\ Z The end of the target or the line break at the end (independent from the multiline mode) indicates the position 2.
\ Z The end of the target (independent from the multiline mode) indicates location 2.
\ G The first matching position in the target
A, Z, z, and G are rarely used.

These assertion tests are based on the current location, and the assertion also supports more complex test conditions. More complex assertions are represented in sub-mode, including Lookahead assertions and Lookbehind assertions ), these assertion judgments are only matching judgment conditions, which are not recorded in the matching results and do not match characters.

Lookahead assertions)

Predicate, usually expressed (? = Pattern), from the current match position to test whether the matching string is true after the match, and (?! Pattern). Let's look at an example. Source string: "abc100", regular expression:
/[A-z] + (? = \ D +)/, the analysis process is as follows:

First, the regular expression character [a-z] + gets control. The matching character is "abc", and the position starts from "0" and changes to 3. Test whether/d + is set up at this position. Returns true if it matches 100 characters. Therefore, the regular expression is successfully asserted. Returns the matching string "abc"

(?! Pattern) is a forward match. If no match is successful, the system returns the true result. The test result is as follows:

Lookbehind assertions)

Assertion, the common expression is :(? <= Pattern) or (? <! Pattern) format. In a regular expression, do not show unfixed-length quantifiers. an endless loop may occur. An error occurred while matching. It indicates that a matching character will appear on the left of the current position, returns the true result, and the matching is normal later. If it appears on the leftmost side, the default position starts from 0 and the matching fails. Generally, the regular expression starts matching and then traces back until the matching ends. Let's take a look at the following example: the source string: "abc100 + =", the regular expression is :"(? <= \ W) \ w + ". The matching process is as follows:

First, the control is obtained by the regular expression character/\ w +/. The matching character is "abc100", and the position starts from "0" and matches to 6 characters. An error occurred while detecting the left change \ w match from the position 0. Therefore,/\ w +/matches "bc100" from character B and tests whether it has the character "a" on the left. The reverse assertions are correct. Therefore, it matches the string "bc100 ",(? <! Pattern), but returns true if no matching is successful!

Postscript: from this article, we found that the search features are from left to normal. Generally, positive assertions are placed before regular expressions, and reverse assertions are placed before matching regular expressions. However, it can also be placed before or after. Here we will not give an example. Welcome to 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.