Using Regex Look Arounds
.net supports four types of "Look Around". These can be used to make sure that patterns do or do not appear before or after whatever it is you're matching on. Consider the following text:
"my car will be off the road for 2 days. it's reg num is ab123abc"
If I wanted to pluck out the registtration number, I'd probably start with an expression like
"(?<regnum>[a-z]{2}.?[0-9]{1,3}.?[a-z]{3})"
The problem is, although that expression does indeed return ab123abc as a match, it also returns or 2 day. Using a negative lookbehind and a negative lookahead I can stop the RegEx engine from matching parts of words.
The following expression includes the negative look behind (?<![a-z]) and the negative look ahead (?![a-z]))
"(?<regnum>(?<![a-z])[a-z]{2}.?[0-9]{1,3}.?[a-z]{3}(?![a-z]))"
This only matches on the regnum.
The available "Look Around" expressions are:
| ?= |
Positive Look Ahead |
| ?<! |
Negative Look Behind |
| ?<= |
Positive Look Behind |
| ?! |
Negative Look Ahead |
| Author |
: Paul Hayman |
| Published |
: Monday, 18 September, 2006 |
Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services.
Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.