Re: Regex advice

[email protected] (Gunnar Hjalmarsson)
Newsgroups perl.beginners
Message-ID <[email protected]>
[ Please don't remove attributions. ]

Steve Bertrand wrote:
> Gunnar Hjalmarsson wrote:
>> Steve Bertrand wrote:
>>> if 
>>> (/.*simscan~\[\d+\]~([\w|\s]+).*?~(\d+\.\d+)s~.*?~(.*?)~(.*?)~(.*)/) {
>> -------------------------^^^^^^^^^^
>> This captures the "result", but with a trailing space. You may want to 
>> let the capturing parentheses be followed by \s to prevent that.

With "captures the result" I mean captures what's later going into the 
$result variable. Since a quantified subpattern such as

     ([\w|\s]+)

is greedy by default, it would capture

     "CLEAN "

or

     "SPAM REJECT "

i.e. with trailing spaces. I simply suggested that you add a \s, i.e.

     ([\w|\s]+)\s

to rather capture "CLEAN" or "SPAM REJECT".

>>>                 $result   = $1 if $1;
>>>                 $scantime = $2 if $2;
>>>                 $ip       = $3 if $3;
>>>                 $from     = $4 if $4;
>>>                 $to       = $5 if $5;
>>
>> The statement modifiers seem redundant to me.

"if $1" etc. are statement modifiers; see "perldoc perlsyn".

I mean that since the regex returned a true value, we know already that 
all the dollar-digit variables are defined. Sure, $3, $4, or $5 might 
just be the null string, but - without knowing what the rest of the 
program looks like - I assume that you ought to assign it to respective 
variable anyway.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.