[ale] perl regexp question de'jour ;-)

Jim Popovitch jimpop at yahoo.com
Fri Jul 7 16:47:42 EDT 2006


OK,

This time it's not a shell script challenge, but a simple perl regexp 
question (challenge?)....

I need to parse an options config file which contains plaintext 
parameters.  My perl script will process that file and do some foo with 
the params it gleans.

An example of some lines in the options file looks like this:

    ----------------------
    alert bob at domain.com 6
    alert sue at domain.com once
    alert joe at domain.com always
    ----------------------

Basically I open the options file and parse each line, one step is to
evaluate each line in if/elsif blocks, detecting the presence of a 
keyword (in this case "alert") then parsing the params.  So, for the 
above example, I have this catch:

    ------------------------
    }
    elsif ($ln =~ /^alert\s(\S+)\s(\S+)/i) {
       my $email = $1
       my $interval = $2
       ....
    }
    ------------------------

The above code works fine.  However, I would like to change it so that 
the "alert" statement can contain more or less params.  For instance, if 
the option line contained just "alert jim at domain.com" then the interval 
would default to "always", additionally I would like to allow for an 
option line like "alert jim at domain.com always high" where 'high' 
signifies a priority.

My problem is this:  Perl regexp matching seems to not be flexible (or 
my understanding of perl and/or regexp matching isn't broad enough) in 
that it doesn't allow me to specify a match condition for more 
parameters than actually exist.  The above code will work as long as 
there are at least 2 params following the keyword "alert".  If I use the 
following code, then option lines with less than 3 parameters are skipped.

    ------------------------
    }
    elsif ($ln =~ /^alert\s(\S+)\s(\S+)\s(\S+)/i) {
       my $email = $1
       my $interval = $2
       my $priority = $3
       ....
    }
    ------------------------

Any examples on how to use perl regexp matching against a variable 
number of parameters?

Thx,

-Jim P.






More information about the Ale mailing list