Eric Bohlman <eb******@earthlink.net> writes:
Actually you can use positive lookahead to implement an arbitrary "and":
/^(?=.*this)(?=.*that)/ (a trick introduced in the _Perl Cookbook_ and
implemented in a Perl module of mine).
The problem is that you can only do this efficiently at the end of a string.
Compare this for "or':
/z(aa|bbb)cd/
If we had the hypothetical & operator, and wrote
/z(.*this.*&.*that.*)cd/
then we wanted the part between "z" and "cd" to contain both "this"
and "that".
If you do that with lookahead, you need to be able to bound the search
somehow, or the lookahead can test past the cd. As your example:
/z(?=.*this)(.*that.*)cd/
would incorrectly match
"z that cd this"
You need to ensure that the lookahead is only tested against the same
string as the other argument to "and".
You can do "the trick" and duplicate the continuation:
/z(?=.*this.*cd)(.*that.*cd)/
but even that can be broken by using more complex expressions. Take
"all digits, and at least three 4's":
/z(\d*&(.*4){3}.*)cd/
Doing the trick here gives
/z(?=\d*cd)(.*4){3}.*cd/
However, that also matches
"z111cd444cd"
Again, you have to build your RegExps so the lookahead is bounded,
something that was not necessary with the hypothetical "&" operator.
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'