[Omaha.pm] Perl regexp problem...

Andy Lester andy at petdance.com
Sat May 13 23:14:51 PDT 2006


On Sun, May 14, 2006 at 12:37:29AM -0500, Daniel Linder (dan at linder.org) wrote:
> For example, this line:
> 
> ^var1=(?i)(.*)(5705)(.*)
> 
> will find all lines that begin with "var1=", followed byt any
> characters, then 5705, then any other characters.  This filter works
> fine.

Actually, that breaks down to

    ^var1=      beginning of the line, and then "var1="
    (?i)        an optional "i"
    (.*)        any string
    (5705)      the string "5705"
    (.*)        any other string

The (.*) at the end is useless because it will always match in this
case, and at the end of the line, you don't care.


> What I want to do is write a filter that finds the opposite lines; i.e all
> lines that begin with "var1=" but do not have 5705 in their
> value.  I tried
> 
> ^var1=(?i)(.*)(^5705)(.*)
> 
> but that didn't work...

That's because the ^ is only negation in a character set, as when you
say [^aeiou] to mean "any character except the vowels".

Probably the simplest way to do this is to have two regexes.

    if ( $line =~ /^var1=/ && $line !~ /5705/ ) {

This says "If it starts with var1=, and it doesn't match 5705 anywhere",
then $line matches.

xoa
-- 
Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance


More information about the Omaha-pm mailing list