# Explanation of assertion with Zero Width

Source: Internet
Author: User

0-width assertion means (the matching width is zero and certain conditions/assertion are met) I do not know that this word was invented by the bastard. It is just too easy.

The zero-width assertion is used to find the anchor before or after some content, that is, they are anchor such as \ B ^ \$ \ <\>, it is used to specify a position, which must meet certain conditions (that is, assertion). Therefore, they are also called assertion with zero width. Assertions are used to declare a fact that should be true. In a regular expression, matching continues only when the assertions are true.

Zero-width assertions are classified into four types:

1)

The first assertion is also called the zero-width positive prediction first assertion (? = Exp) -- indicates the position before the matching expression.

For example, [A-Z] * (? = Ing) can match cook and sing in cooking and singing.

Note: The execution step of the first assertion is to first find the first ing (the expression in the first assertion) from the rightmost end of the string to be matched and then match the expression above it, if the match fails, search for the second ing and match the string before the second ing. If the match is successful, match the regular expression.

For example :.*(? = Ing) can match "cooking sing" in "cooking singing" instead of "cook ".

2)

The post-development assertions are also called the zero-width positive review post-development assertions (? <= Exp) -- indicates the position behind the matching expression.

For example (? <= ABC). * matches defg in abcdefg.

Note: The execution steps of the Post-upload assertions are the opposite of those of the first assertions: first, locate the first ABC (that is, the expression in the first assertions) from the leftmost end of the string to be matched) then match the expression following it. If it cannot match, search for the second ABC and then match the string following the second ABC. If it can match, match.

For example (? <= ABC). * matches defgabc in abcdefgabc instead of abcdefg.

3)

Assertion with negative Zero Width

Assertion (?! Exp) also matches a zero-width position, but the "assertion" of this position takes the inverse value of the expression, for example (?! Exp) indicates the position before "exp". If "exp" is not true, it matches this position. If "exp" is true, it does not. Similarly, there are two types of negative 0-width assertions: "first" and "post-release". The negative 0-width post-release assertions are (? <! Exp)

Assertion after negative zero width (? <! Exp)

Negative 0-width first assertion (?! Exp)

The assertion of negative and zero-width must be the same as that of positive.

Common grouping syntax
Category Code/syntax Description
Capture (Exp) Match exp and capture text to automatically named group
(? <Name> exp) Match exp and capture the text to the group named name. You can also write (? 'Name' exp)
(? : Exp) Matches exp, does not capture matched text, and does not assign group numbers to this group
Assertion with Zero Width (? = Exp) Match the position before exp
(? <= Exp) Match position after exp
(?! Exp) The position behind matching is not exp
(? <! Exp) Match the position that is not exp
Note (? # Comment) This type of grouping does not affect the processing of regular expressions. It is used to provide comments for reading.

Example:

Cat File

Aaa bbb cd= "123" FD

To retrieve the value after CD:

1. Grep-op '(? <= Cd = ") \ D + 'file

[Resolution]

Use Cd = "as the post-development asserted to match multiple numbers behind it.

Cat File

RX optical power:-5.01dbm, TX optical power:-2.41dbm

The values of the several decibels to be retrieved:

-5.01

-2.41

1. Grep-op '(? <= :).*? (? = D) 'file

[Resolution]

Then the asserted ":" The following string until the string before the D character ,? To avoid greedy matching.

Cat File

["Check_ssh", OK], ["check_eth", OK], ["check_disk", OK], ["check_swap", OK], ["check_mem", OK], ["check_hardware", false], ["check_filesystem", false], ["check_port", OK], ["check_redis-server", OK], ["check_login", OK]

Retrieve the content with the "false" in:

Check_hardware

Check_filesystem

1. Grep-po' [^ "] + (? = ", False) 'file

Explanation of assertion with Zero Width

Related Keywords:

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.