Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

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;
0
David Aldridge
Asked:
David Aldridge
  • 4
  • 4
1 Solution
 
jmcgOwnerCommented:
Your pattern in $critical has commas in it while your @data does not. That would be enough to prevent a match.
0
 
David AldridgeAuthor Commented:
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'.
0
 
David AldridgeAuthor Commented:
ok, I figured it out.. duh.. :)  supposed to be $critical =~ /$fs/i  

Thanks anyway!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jmcgOwnerCommented:
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.
0
 
David AldridgeAuthor Commented:
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.  :)

0
 
jmcgOwnerCommented:
I changed one line of your script to use 'index':

#!/perl -lw
$server = "server";
$status = "Critical";
$critical = "dsmc,,,";
my @data = "dsmc UNIX P3";
     
     foreach $line (@data)
     {
          ($fs, $msg) = $line =~ /(.*?)\s(.*)/;
          chomp($fs);
          if ( index( $critical, $fs) >= 0 )  #### I changed this line
          {
               print("$server *TEST* $msg $server - $critical $status");
               exit;
          }
     }
     print("$server *TEST* $critical not found in list. No ticket generated.");
     exit;


==================

As for perversity, I'm not sure how to solve that, since this clearly is just a snippet from some larger problem. The value of $critical seems odd -- what with all those commas -- and it's much more commonly seen that the script contains a pattern that is used to find matches on lines read in rather than the file containing patters that are matched against strings built into the script.
0
 
David AldridgeAuthor Commented:
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
0
 
jmcgOwnerCommented:
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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now