lookaround assertions across languages

/(?<!->)\bparentDataStore/ ## is a perl regex with a negative lookbehind. It disqualifies "->parentDataStore"

(class|struct)\s+MyClass\b(?!;) ## is a perl regex for a class definition. The trailing negative lookahead (?!;) ensures we don’t match a forward class declaration.
—-Below is a Nov 2010 java example:

replaceAll("(?<=</?)MTSMessage", "SIG_Notification")

The optional positive lookbehind assertion above says to match (and replace) the “MTSMessage” string provided it’s preceded by “<” or “</”

LookAhead is simpler than LookBehind — Compare the syntax. Some languages only support lookAhead.

I feel negative lookAhead is more useful than positive lookAhead. I feel these zero-width assertions are useful in progressive matches, but I seldom need complex progressive.

If you use lookaround you may want to start with sample code and make incremental changes. Plausible but incorrect lookaround patterns abound. This is a time you need to understand how regex engines work.

* explains how (not) to capture a back-reference in a lookAhead.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s