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

Source: Internet
Author: User

Address: http://www.cnblogs.com/mu-mu/archive/2013/02/06/2893581.html

 

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.

 
VaRReg =NewRegEx (@"\ W + (? = Ing)");VaRSTR ="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 followingCode

 
VaRReg =NewRegEx (@"A (? = B) c");VaRSTR ="ABC"; Console. writeline (Reg. ismatch (STR ));//Returns 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.