David Aldridge
asked on
Regex problem
I have the following..
#!/perl -lw
$server = "server";
$status = "Critical";
$critical = "dsmc,,,";
my @data = "dsmc UNIX P3";
foreach $line (@data)
{
($fs, $msg) = $line =~ /(.*?)\s(.*)/;
chomp($fs);
if ($fs =~ /$critical/i)
{
print("$server *TEST* $msg $server - $critical $status");
exit; # <--------- It SHOULD exit here, but the regex doesn't appear to match..
}
}
print("$server *TEST* $critical not found in list. No ticket generated.");
exit;
#!/perl -lw
$server = "server";
$status = "Critical";
$critical = "dsmc,,,";
my @data = "dsmc UNIX P3";
foreach $line (@data)
{
($fs, $msg) = $line =~ /(.*?)\s(.*)/;
chomp($fs);
if ($fs =~ /$critical/i)
{
print("$server *TEST* $msg $server - $critical $status");
exit; # <--------- It SHOULD exit here, but the regex doesn't appear to match..
}
}
print("$server *TEST* $critical not found in list. No ticket generated.");
exit;
Your pattern in $critical has commas in it while your @data does not. That would be enough to prevent a match.
ASKER
But if $fs has 'dsmc' as its value, and $critical has 'dsmc,,,' as its value, why doesn't a
regex of $fs =~ /$critical/i match? I don't want an exact match, just match the string
'dsmc'.
regex of $fs =~ /$critical/i match? I don't want an exact match, just match the string
'dsmc'.
ASKER
ok, I figured it out.. duh.. :) supposed to be $critical =~ /$fs/i
Thanks anyway!
Thanks anyway!
I guess that'll work, but based on what you've shown us so far it's a fairly perverse way to go about it.
Regular expressions are great, but when you're checking for an exact sub-match to a string, you're slightly better off using the 'index' operator.
Regular expressions are great, but when you're checking for an exact sub-match to a string, you're slightly better off using the 'index' operator.
ASKER
Can you show me how you would plug the index operator in the above script? I'm new to perl and am ALWAYS looking for 'less perverse' ways of doing things. :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, I didn't want to post all of the script... its a bit large, so that might account for the oddness. $server, $status and $criical are passed to the script with arguments. @data is read from a text file. One question though.. is the index function not case sensitive?
Thanks for you help and advice!
holein5
Thanks for you help and advice!
holein5
You're right, I missed the need for a case-insensitive match.
You could do:
if ( index( lc $critical, lc $fs) >= 0 )
but that may start to lose any performance advantage vs the regular expression match. You could omit the 'lc' operator for any string that you already knew for certain didn't need it, such as a string supplied in the script itself.
You could do:
if ( index( lc $critical, lc $fs) >= 0 )
but that may start to lose any performance advantage vs the regular expression match. You could omit the 'lc' operator for any string that you already knew for certain didn't need it, such as a string supplied in the script itself.