8

Recently, I have been seeing "zero width elements" in regular expressions. What are they? Can they be treated as ghost data, so that for replacement, they won't be replaced, and for ( ) matching, they won't go into the matches[1], matches[2], etc?

Is there a good tutorial for all its various uses? Have they been here for a long time? Which version of O'Reilly's Regular Expression book was the first to discuss them?

Adam Katz
  • 14,455
  • 5
  • 68
  • 83
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • Just to make sure we're on the same page, are you talking about assertions like the ones documented here? http://msdn.microsoft.com/en-us/library/h5181w5w(v=VS.71).aspx – Welbog Nov 25 '10 at 20:56
  • yes, and the `?!`, `?<!`, `?<=` – nonopolarity Nov 25 '10 at 20:57
  • Well there's a lot of those. Is there something specific you're having trouble with? If not I'd suggest you check out the documentation on assertions (http://msdn.microsoft.com/en-us/library/bs2twtah(v=VS.71).aspx and the link in my comment above - for .NET anyway). Are you working with .NET regular expressions or some other dialect? – Welbog Nov 25 '10 at 21:02
  • O'Reilly has [several books on regex](https://ssearch.oreilly.com/?q=regular+expressions), but I assume you mean [Mastering Regular Expressions](http://shop.oreilly.com/product/9780596528126.do) (currently in its 3rd edition, 2006) or else [Regular Expression Pocket Reference](http://shop.oreilly.com/product/9780596514273.do) (currently in its 2nd edition, 2007). – Adam Katz Sep 01 '16 at 17:49

1 Answers1

8

The point of zero-width lookaround assertions is that they check if a certain regex can or cannot be matched looking forward or backwards from the current position, without actually adding them to the match. So, yes, they won't count towards the capturing groups, and yes, their matches won't be replaced (because they aren't matched in the first place).

However, you can have a capturing group inside a lookaround assertion that will go into matches[1] etc.

For example, in C#:

Regex.Replace("ab", "(a)(?=(b))", "$1$2");

will return abb.

A very good online tutorial about regular expressions in general can be found at http://www.regular-expressions.info (even though it's a little out of date in some areas).

It contains a specific section about zero-width lookaround assertions (and Part II).

And of course they are covered in-depth in both Mastering Regular Expressions and the Regular Expressions Cookbook.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561