On 31 May 2006,
wrote:
> You exactly got the point: I want the user to rewrite the
> Pattern. The question is, how to write a *negated* pattern using
> Perl RE Syntax?
You can do it for some cases, but because of limitations on memory and
CPU cycles, most complex regexes can't be inverted in a reasonable
amount of time. When there's code inside, it gets even worse.
Look at the book "Higher-Order Perl" by Mark-Jason Dominus. It has a
long section on finding all the strings that can match a given regular
expression; if you read it carefully you'll see why inverting a
regular expression is generally a hard problem, just as producing all
the strings that match it.
Note also that if security is a concern, giving users regexp access is
equivalent to letting them run any code due to the code escapes
possible in Perl's regex interpreter. It may be simpler to give the
users a limited language with a NOT operator. Parse::RecDescent has
some good examples of this kind of parser in the distribution. The
users may also prefer this to the raw power of regexps, and it's what
I would do for a production system.
> Of course, one hack for my original problem would be to "invent" a
> special character (say, exclamation mark) which is allowed to be at
> the very start of the expession, and just has the meaning "pattern
> has negated meaning".
Yes

That would be easiest.
>> I honestly don't see a reason why you shouldn't provide a -v option,
>
> The reason is because I simplified the problem very much so to make
> it better feasible to discuss here. The interesting point for me is
> not finding out whether the negation effect can be done solely
> within the pattern, or has to be "moved outside" to the distinction
> between =~ and !~, or if/unless construct.
It should be moved outside, so you can go on to finish the project
Ted