in reply to Regex frustration

This strikes me as a startlingly unusual way to do something standard. I take it you mean to use $exp as a matching expression and then use variable interpolation to get $1 into that string. See, here's the thing. You're working too hard and it's loads easier to make that work correctly. Your original string is interpolating $1 in before the regular expression is even executed. The critical change is to either use non-interpolating single quotes or to escape the $1 as \$1. Your first use of eval is completely superfluous and reduces overall legibility.

# Use a regex object so it's compiled only once $exp = qr|^DMSC0022\s+\S+\s+(\w+)$|; # Use a non-interpolating string (or escape the $1 as \$1 $str = 'Logfile_Connection_Lost Hostname: $1'; # I can only assume you are assigning to $_ # because this is a snippet from some larger code. On it's # own this is really weird. Normal code would do something # like "DMS ..." =~ $exp instead. $_ = "DMSC0022 X10 oswald"; # Force $exp to execute (don't just eval it, this is more correct) if ( $_ =~ $exp ) { # the eval() is still needed to force the source code # to generate. This is still silly. eval "print \"$str\""; }
Myself? Try this:
$exp = qr/\w+$/; $str = "Logfile_Connection_Lost Hostname:"; $input = "DMSC0022 X10 oswald"; ($hostname) = $input =~ m/$exp/g; print "$str$hostname" if defined $hostname;

That's more legible and will run faster as well.

Update: I completely rewrote the node since it sucked initially.

Replies are listed 'Best First'.
Re: Re: Regex frustration
by BrowserUk (Patriarch) on Sep 25, 2002 at 01:38 UTC

    Did you test this? It doesn't work on my system.

    C:\test>type fred.pl $exp = qr|^DMSC0022\s+\S+\s+(\w+)$|; $str = 'Logfile_Connection_Lost Hostname: $1'; # note that you have to + use single quotes or the $1 is interpolated as soon as you de fine $str, not when you eval() the string (which is completely superfl +uous) $_ = "DMSC0022 X10 oswald"; if ($exp) { print eval $str; # *now* get $str to interpolate } C:\test>fred Use of uninitialized value in print at C:\test\fred.pl line 6. C:\test>

      No, I'm an idiot and didn't. What I initially wrote was buggy and didn't work. Two problems: the expression wasn't being evaluated properly and the eval wasn't done correctly. The *right* way to do that eval (though an eval() isn't right in this case at all) was to do eval "print \"$str\"";.

      So my bad. I fixed the node so it's not buggy and has some actual good advice.

Re: Re: Regex frustration
by Anonymous Monk on Sep 25, 2002 at 02:42 UTC
    You're right. the original code is a beast and the regex's are read from a db table. There are hundreds mostly unique some which just require a match, but most use $1, $2, $3, etc. My example was something simple in which I tried to convey the problem. While, your first suggestion may sound silly, I think it is the right fit in this case.

      Too bad for you then I guess. I'll just send a few commisseration thoughts over your way for when you're stuck doing code maintenance on that thing. :-(

      Update: Added a frownie