perlpal has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im doing a pattern match on a particular string and extracting a part of it to the pattern matching variable $1. Im repeating the same pattern match in a later part of the code ditto but it fails and $1 does not contain anything . Please could you help. Thanks in Advance!

Replies are listed 'Best First'.
Re: Repeated Pattern Matching Fails
by Corion (Patriarch) on Jan 28, 2009 at 12:14 UTC

    The problem is with the 42nd character in your regular expression.

    You can help us to help you better. Please post a small self-contained program, together with the input data and output you expect, and also tell us how it behaves and how that behaviour differs from what you expect. That way, we can point out concrete improvements and guide you towards the most efficient help. Otherwise, we can only provide you with vague indications as to what could be a possible cause.

Re: Repeated Pattern Matching Fails
by planetscape (Chancellor) on Jan 28, 2009 at 12:13 UTC

    And your code looks like ... ?

    HTH,

    planetscape
      Oops!im really sorry bout that. The code is mentioned below : -------------------------------------------
      $_sensor_type = “I2600”; if($_sensor_type =~ /[a-z]+([0-9]+)/ig){ print "\n\n\n***Importing Original Action sens +or Configuration onto the ISM and pushing it onto the sensor model $1 + via API***\n\n\n"; my $out = `wget --no-check-certificate http:/ +/$_config_hash->{'database_ip'}/intruvert/jsp/sensorConfigImport.jsp? +params=$_config_hash->{'sentype'}:$_config_hash->{'sensor_ID'}:Hampi_ +$1_Master_Sigset.xml`; }
      ---------------------------------------------------
      What im trying to do here is push an API depending on certain parameters.
      The issue lies with the "if" statement in the beginning. The pattern match fails due to which i dnt get the value "2600" in $1.
      However, this code is used in the starting of the program too where it works fine. The issue arises only when it is used the second time in the code.
      Hope i've mentioned all the details required this time around :)

        You are using the /g option on a pattern in boolean context which means that the first time it will match and the second time it will continue searching where the first match ended and will therefore not find another match and will return "false".

        If you look closely at your "$_sensor_type =" statement, you will see that you have different kinds of quotes around "I2600". Notice the difference between them and the first quote in your print statement, ". forward leaning and backward leaning " are different than normal straight up ".

        I am surprised that this even compiles. Are you using warnings and strict?

        Other than that, I see no reason why I2600 shouldn't match the regex, although the /g is completely unnecessary here. I tested "I2600" on my machine and works great with proper quote characters.

        UPDATE: this is not the problem, it appears that previous use of string in an inappropriate global match was the issue..oooops. Although I must say that sometimes these non-printing and/or non-standard characters can and DO cause problems... that is not the case here..

Re: Repeated Pattern Matching Fails
by Anonymous Monk on Jan 28, 2009 at 12:37 UTC
    If you complaining that the regular expression did not match the second time, then the answer is "something changed".

    If you complaining that $1 has been overwritten, then you really should save $1 into a named variable immediately after the RE.