Re: Negated Perl Regexp



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
.



Relevant Pages

  • Re: string.remove bringt nicht das erwartete
    ... Ein Regex ist da etwas flexibler. ... pattern, "", System.Text.RegularExpressions.RegexOptions.IgnoreCase) ... Wenn man nun aber nach einem Backslash sucht ... gleichen gilt für alle anderen Metazeichen. ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Regex doubt
    ... > really dont want to use HtmlAgilitypack - I'm learning RegEx and want to ... >>> exp is braindead as it chokes on nested tags. ... >>> says something like - 'match any text that doesnt match this ... >>>, within a pattern, match the pattern itself?In that case, the first ...
    (microsoft.public.dotnet.framework)
  • Re: limits on regex?
    ... Same regex could work and fail on 2 different strings. ... not on the complexity of regex expression. ... storing / changing documents in DB and keep resulting pattern description ... > Does anyone have any hard data on the size / complexity limits of the ...
    (microsoft.public.dotnet.framework)
  • Re: How many differences, categories?
    ... perception.....The act of remembering and the act of perceiving both ... detect a pattern in a vary large choice of possible patterns....When we ... If the act of remembering and the act of perceiving both ... To translate or transliterate. ...
    (sci.cognitive)
  • (patch for Bash) match, strinterval
    ... These 2 builtin commands can do regex or fixed-string matching. ... Return success if STRING contains REGEX pattern. ... which case SUBMATCH will contain 3 elements, ...
    (comp.unix.shell)

Loading