I'm converting some Perl code to Ruby and for the most part it's gone
well, until I hit this problem...
I've got some files full of regex expressions that are read in by a Perl
program and eval'ed (actually the eval is doing a pattern match against
$_) in order to determine if a particular file matches the criteria in the
regex. It works like this (warning, Perl code ahead :) :
$expression = "!(/Error/ || /Abort/ || /[1-9] error/ || /fatal/)"
#actually $expression was read from one of many (thousands) of files
open(RPT,"reportfile) or die ;
while (<RPT>) {
if(eval "$expression") {
$found = 1;
#other stuff....
}
}
So 'eval "$expression"' returns a true value if $expression evaluates to
true (in the Perl sense of truth).
Now, I'm finding that something like:
if (line =~ !(/Error/ || /Abort/ || /[1-9] error/ || /fatal/) )
just isn't going to work in Ruby for various reasons. Technically, the
strings in $expression (above) are not regular expressions, they are
formulas that contain regular expressions.
Is there any way to do this in Ruby - I really don't want to have to
change potentially thousands of files which contain:
!(/Error/ || /Abort/ || /[1-9] error/ || /fatal/)
to:
!/Error|Abort|[1-9] error|fatal/
(and actually, I think the '!' would still be a problem)
Phil