Solution looking for a problem? Lookahead and Lookbehind
Archive - Originally posted on "The Horse's Mouth" - 2012-06-30 16:42:58 - Graham EllisThere are some things that it's just plain difficult to find good, practical examples to show delegates on training course. "Solutions looking for problems" I call them sometimes. And one such group are lookahead and lookbehind in regular expression.
However, I think I did a bit better yesterday ...
Take the lines
2598 LOT BL6 4JP SD 673086 53.5729974812 -2.4937754345 Lostock Parkway 118752 125475 126934 140033 208136 209168
and
1090 AVP NULL NULL NULL NULL Aylesbury Vale Parkway NULL NULL NULL NULL 13042 49212
and a requirement to match "Parkway" and the following number, BUT ONLY if the line doesn't contain the word NULL later on.
Here's the demonstration solution - using negative lookahead:
if (preg_match('/\t([^\t]+Parkway)(?!.*NULL)\t(\d+)/',$line,$gotten)) {
Sadly, I still advocate that it's better to perform this matching in two simpler regular expressions rather than one more complex one. Two easier statements will be quicker to code and test, easier to understand and update later, and will probably run faster too:
if (preg_match('/Parkway.*NULL/',$line)) continue;
if (preg_match('/\t([^\t]+Parkway)\t(\d+)/',$line,$gotten)) {
Full source code - [here].