Assertion with zero-width Regular Expression (assertion with zero-width positive prediction first)

Source: Internet
Author: User

Recently, in order to process the source code of html files, regular searches and replacement are required. So I took this opportunity to learn about the regular expression system. Although I used regular expressions before, I learned how to get through them temporarily. In the course of learning, I still encountered many problems, especially the zero-width assertion (I want to talk about it here. The content copied and pasted on the internet is everywhere, I encountered a problem and checked a lot of repeated things. Sweat !!!), So write down your understanding here for future reference!

What is positive prediction with Zero Width first? refer to the official explanations on msdn.

(? = Subexpression)

(0-width positive prediction first asserted .) The child expression continues matching only when it matches the right side of the position. For example,\ W + (? = \ D)Match the word that is followed by a number instead of the number.

Classic example: if a word ends with ing, You need to obtain the content before ing.

Var reg = new Regex (@ "\ w + (? = Ing) "); var str =" muing "; Console. WriteLine (reg. Match (str). Value); // returns mu

The above is an example that can be seen everywhere on the Internet. Here you may understand that the original content is the content before the exp expression.

Let's look at the following code:

Var reg = new Regex (@ "(? = B) c "); var str =" abc "; Console. WriteLine (reg. IsMatch (str); // return false

Why is false returned?

In fact, the official definition of msdn has been mentioned, but it is very official. Here we need to pay attention to a key point: this location. That's right. It's a location, not a character. The second example is understood based on the official definition and the first example:

Because a is followed by B, the matching content a is returned at this time (according to the first example, only a does not return the exp Matching content). At this time, (? = B) (? = B) partially solved. Next we need to solve the matching problem of c. Where should we start to match c from the string abc? According to the official definition, we can see that it starts from the position of the subexpression to the right, that is, starting from the position of B, but B does not match (? = B) c for the remaining part of c, so abc does not match (? = B) c.

So how should we write regular expressions if we want to match them?

The answer is: (? = B) bc

Of course, some people will say that abc will be matched directly. Is it still so hard? Of course, you don't need to worry about it. It's just to illustrate how the 0-width positive prediction asserted first? The same principle applies to other zero-width assertions!

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.