olivierp has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks !
I'm having trouble building a regex that would match as follows:
string starts with chars, contains this, but does not contain that and does not end with whatever A couple of samples:
^chars(?!.*that).*this(?!.*that).*
If I include the whatever part, as here
^chars(?!.*that).*this(?!.*that).*(?!.*whatever)$
, sample 2 still matches, whereas this:
^chars(?!.*that).*this(?!.*that).*[^w][^h][^a][^t][^e][^v][^e][^r]$
excludes sample 2 but is horrible...
Am I missing something with the negative lookahead, or is it not possible to do such a thing ?
--
Olivier
Edited by demerphq: Changed </br> to <br />
string starts with chars, contains this, but does not contain that and does not end with whatever A couple of samples:
- chars at the beginning, this is right and whatever is not at the end
- chars at the beginning, this is ok and the end is whatever
- chars at the beginning, this is ok and that is included
- chars at the beginning, that here this is not ok
^chars(?!.*that).*this(?!.*that).*
If I include the whatever part, as here
^chars(?!.*that).*this(?!.*that).*(?!.*whatever)$
, sample 2 still matches, whereas this:
^chars(?!.*that).*this(?!.*that).*[^w][^h][^a][^t][^e][^v][^e][^r]$
excludes sample 2 but is horrible...
Am I missing something with the negative lookahead, or is it not possible to do such a thing ?
--
Olivier
Edited by demerphq: Changed </br> to <br />
|
---|
Replies are listed 'Best First'. | |
---|---|
•Re: Problem with negative lookahead at end of string
by merlyn (Sage) on Jun 21, 2004 at 15:45 UTC | |
by melora (Scribe) on Jun 21, 2004 at 17:35 UTC | |
Re: Problem with negative lookahead at end of string
by tye (Sage) on Jun 21, 2004 at 15:12 UTC | |
Re: Problem with negative lookahead at end of string
by delirium (Chaplain) on Jun 21, 2004 at 15:25 UTC | |
by runrig (Abbot) on Jun 21, 2004 at 17:40 UTC |
Back to
Seekers of Perl Wisdom