Re: Regex advice
[email protected] ("John W. Krahn")
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
Steve Bertrand wrote:
> Hi everybody,
Hello,
> I am far from a regex guru, so I know that I can get advice on how to
> learn to improve my regex knowledge so I can better my currently working
> code to protect against failure in the future. I would appreciate some
> criticism, with explanations as to the changes if possible.
>
> I've made it as brief as possible, with hopefully enough context (sorry
> if it wraps):
>
>
> while (<LOG>) {
>
> if (/.*simscan~\[\d+\]~([\w|\s]+).*?~(\d+\.\d+)s~.*?~(.*?)~(.*?)~(.*)/) {
The .* pattern at the beginning is useless as it causes unnecessary
back-tracking, just remove it. The character class [\w|\s] includes
both the '|' and '_' characters but it doesn't appear that that field
would normally contain either a '|' or a '_' character.
> $result = $1 if $1;
([\w|\s]+) will always match at least one character so the only way that
$1 can be false is if it contains '0'.
> $scantime = $2 if $2;
(\d+\.\d+) will always match at least three characters so it can never
be false so the test is superfluous.
> $ip = $3 if $3;
> $from = $4 if $4;
> $to = $5 if $5;
If $3, $4 or $5 are false they will contain either '' or '0'.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall