Re: Negated Perl Regexp
- From: Ted Zlatanov <tzz@xxxxxxxxxxxx>
- Date: Tue, 30 May 2006 12:27:10 -0400
On 30 May 2006, ro.naldfi.scher@xxxxxxxxx wrote:
Background of what this is needed for: I'm writing tiny utilities in
Perl, which act as a filter for input text. Typically, the core of
the "program" contains something like
/$PATTERN/ && print(transform($_))
i.e. read all lines from stdin, and if they match some pattern,
print out a transformed version of the line. The is supplied via
ARGV. This works fine, but I also would like the user of this
utility to be able to *revert* the sense (i.e. read all lines from
stdin, and if they DO NOT match the pattern, etc.), like you have
with grep (where the option -v reverts the test).
The keypoint here is that in this particular application, I would
prefer NOT to introduce an option such as grep's "-v" to my utility,
but encode the "negation of the pattern" into the pattern itself.
You either ask the user to rewrite $PATTERN, or you give a -v option.
I don't understand how you would know *when* to negate the pattern
without a -v option.
Is this possible at all within the realm of Perl regular
expressions, or do I have to invent my own workaround (which of
course would be possible)?
Yes usually (for example, it may not work nicely if you have code
embedded inside the regex, and there are many cases that are possible
but computationally very expensive), but it's much more complicated to
invert a regex than to invert the test for that regex.
I honestly don't see a reason why you shouldn't provide a -v option,
or some way for the user to say "invert this pattern", and then act
upon that to invert the test. Maybe you can explain...
Ted
.
- Follow-Ups:
- Re: Negated Perl Regexp
- From: Ronny
- Re: Negated Perl Regexp
- References:
- Negated Perl Regexp
- From: Ronny
- Negated Perl Regexp
- Prev by Date: Re: Another REALLY basic question!
- Next by Date: Re: Unicode filename in Windows
- Previous by thread: Re: Negated Perl Regexp, Howabout qr in Modules?
- Next by thread: Re: Negated Perl Regexp
- Index(es):
Relevant Pages
|
Loading