Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird matching problem...

Status
Not open for further replies.

devilpanther

Programmer
Oct 8, 2004
57
IL
Code:
if ($data =~ /\!/ig)
{
   if ($data =~ /\!about/ig)
   {
      print "1";
   }
}

The above code is acting very weird;
The idea is simple, to match if the string $data contains a ! char, if it is, match a command: !about

But when the code is running, the first match is successful, but the second match is not.

The code only acts as I want when I change it to:
Code:
if ($data =~ /\!/ig)
{
   [COLOR=red]$data_temp = $data;[/color]
   if ([COLOR=red]$data_temp[/color] =~ /\!about/ig)
   {
      print "1";
   }
}

Why is this happening, am I doing something wrong, did I miss some important perl matching rule?


Thank you for your time and help.
 
the 'g' option in your first regexp is causing the problem. I am not sure why that is though.

Why are you doing that anyway, just doing this is the same really:

Code:
if ($data =~ /\!about/ig) {
   print "1";
}

but you don't need the 'g' option there either because you are not looking for all the patterns in the string that match !about, you are only looking for one match then printing "1". The whole thing is probably better written like this:

Code:
if ($data =~ /\!/ and $data =~ /\!about/i) {
   print "1\n";
}
 
I can't do the code as you suggested, because !about is just an example, I have more commands to match.

As for the g, I really don't think it's the problem, because I removed it and the i as well, and still i get the same result.
 
Well... no you're right, the g is the problem.
Thank you so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top