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

Why is this regex not working!
sub FindInHorizontalRegion { print "Inside FindInHorizontalRegion"; my ( $self,$element1,$element2,$duration,$threshold) = @_; my ($xcoord1,$ycoord1, $xcoord2, $ycoord2); my $regex1 = '(.*?)\:(.*?)'; print "Damn the element1 is $element1 \n"; if ( $element1 =~ m/$regex1/g ) { print "Inside wait for image \n"; my $region = $self->WaitForImage( $element1, $duration ); $xcoord1 = $region->getX; $ycoord1 = $region->getY; print "The coordinates of element1 are $xcoord1, $ycoord1 +\n"; } else { print "Inside wait for Text \n"; my $region = $self->WaitForText( $element1, $duration ); $xcoord1 = $region->getX; $ycoord1 = $region->getY; print "The coordinates of element1 are $xcoord1, $ycoord1 \n"; + } if ( $element2 =~ m/$regex1/g ) { my $region = $self->WaitForImage( $element2, $duration ); $xcoord2 = $region->getX; $ycoord2 = $region->getY; print "The coordinates of element2 are $xcoord1, $ycoord1 \n"; } else { my $region = $self->WaitForText( $element2, $duration ); $xcoord2 = $region->getX; $ycoord2 = $region->getY; print "The coordinates of element2 are $xcoord1, $ycoord1 \n"; } my ($x1,$y1,$x2,$y2) = $self->GetHorizontalCoord($xcoord1,$ycoord1 +,$xcoord2,$ycoord2,$threshold); print "The horizontal coordinates are $x1,$y1,$x2,$y2 \n"; print "The element1 is $element1 \n"; if ( $element1 =~ m/$regex1/g ) { print "To check for iImage"; my $region = $self->WaitForImageInROI($element1,$x1,$y1,$x2,$y +2,$duration); return ($region); } else { print "To check for tText"; my $region = $self->WaitForTextInROI($element1,$x1,$y1,$x2,$y2 +,$duration); return ($region); } } sub GetHorizontalCoord { print "Inside GetHorizontalCoord"; my ( $self,$xcoord1,$ycoord1,$xcoord2,$ycoord2,$threshold ) = @_; my $width = $self->GetDeviceProperty('Width'); my $height = $self->GetDeviceProperty('Height'); my $x1 = 0; my $x2 = $width; my $y1 = $ycoord1 - $threshold ; if ($y1<0) { $y1 = 0; } my $y2 = $ycoord2 + $threshold; if ($y2 > $height) { $y2 = $height; } return ($x1,$x2,$y1,$y2); }

The input in the script goes something like this

my $region = $object->FindInHorizontalRegion('SETTINGS:Flight','SETTIN +GS:Tick','1000',10);

I need to pass SETTINGS:Flight to WaitForImageInROI, instead, the if condition is failing and it is invoking WaitForTextInROI. The problem is that in these lines,

my ($x1,$y1,$x2,$y2) = $self->GetHorizontalCoord($xcoord1,$ycoord1,$xc +oord2,$ycoord2,$threshold); print "The horizontal coordinates are $x1,$y1,$x2,$y2 \n"; print "The element1 is $element1 \n"; if ( $element1 =~ m/$regex1/g ) { print "To check for iImage"; my $region = $self->WaitForImageInROI($element1,$x1,$y1,$x2,$y +2,$duration); return ($region); } else { print "To check for tText"; my $region = $self->WaitForTextInROI($element1,$x1,$y1,$x2,$y2 +,$duration); return ($region); }

though element1 is SETTINGS:Flight, the else condition is getting executed. While the same regex works for the previous if conditions, I'm not able to understand why it does not work here.

Thanks.

Replies are listed 'Best First'.
Re: Regex not working
by jethro (Monsignor) on Sep 12, 2011 at 13:27 UTC

    Why do you use the 'g' modifier on the regex? In scalar context this remembers the last search in that variable and continues from there. Useful for searching multiple occurences of a regex pattern, not useful if you want to have the same result each time you search.

Re: Regex not working
by toolic (Bishop) on Sep 12, 2011 at 13:16 UTC
    The 'if' code is executed for me when I run your code (without the $object):
    Inside FindInHorizontalRegionDamn the element1 is SETTINGS:Tick Inside wait for image Can't locate object method "WaitForImage" via package "SETTINGS:Flight +" (perhaps you forgot to load "SETTINGS:Flight"?) at
    Reduce your code further, and post something that any of us can run to view your problem. For example:
    use warnings; use strict; my $regex1 = '(.*?)\:(.*?)'; my $element1 = 'SETTINGS:Flight'; print "Damn the element1 is $element1 \n"; if ( $element1 =~ m/$regex1/g ) { print "Inside wait for image \n"; } else { print "Inside wait for Text \n"; } __END__ Damn the element1 is SETTINGS:Flight Inside wait for image

    See also Basic debugging checklist

      Thank you toolic..please see the reduced version of the code..
      use strict; use warnings; FindInHorizontalRegion('SETTINGS:Flight','SETTINGS:Tick','1000',10); #FindInHorizontalRegion('Flight','SETTINGS:Tick','1000',10); sub FindInHorizontalRegion { print "Inside FindInHorizontalRegion \n" ; my ( $element1,$element2,$duration,$threshold) = @_; my $regex1 = '(.*?)\:(.*?)' ; print "The first element is $element1 \n....The second element is +$element2 \n... Entering if condition for the first time to check if +it is text or image \n"; if ( $element1 =~ m/$regex1/g ) { print "The regex is matched and the first element is an image\n"; } else { print "THe first element is text \n"; } if ( $element2 =~ m/$regex1/g ){ print "The second element is an image \n"; } else { print "THe second element is text \n"; } print "Horizontal coordinates obtained \n"; if ( $element1 =~ m/$regex1/g) { print "To check for Image \n"; } else { print "To check for Text \n"; } }
      The output that I need for this is - The CORRECT OUTPUT

      Inside FindInHorizontalRegion
      The first element is SETTINGS:Flight
      ....The second element is SETTINGS:Tick
      ... Entering if condition for the first time to check if it is text or image
      The regex is matched and the first element is an image
      The second element is an image
      Horizontal coordinates obtained
      To check for Image

      but the output that I am getting is different in the last line.. The output that I GET

      Inside FindInHorizontalRegion
      The first element is SETTINGS:Flight
      ....The second element is SETTINGS:Tick
      ... Entering if condition for the first time to check if it is text or image
      The regex is matched and the first element is an image
      The second element is an image
      Horizontal coordinates obtained
      To check for Text

      The regular espression has to do this, if there is a : in the string, it is taken as Image else Text. The regular works fine the first time, but after Horizontal coordinates are obtained..it prints To check for text instead of printing To check for image. I hope you can understand now.
        As pointed out below by jethro, the problem is that you are using the "g" modifier in your first regex, when you don't really need or want to do that. Remove the "g", and it'll do what you want.
Re: Regex not working
by RichardK (Parson) on Sep 12, 2011 at 13:46 UTC

    Did you intend to match on the single quotes?

    Maybe your regex should be be something like this :-

    my $regex1 = qr/'(.*?):(.*?)'/;

    If there can not be more than one colon then using character classes may be better

    my $r1 = qr/'([^:]*):([^']*)'/;
      Did you intend to match on the single quotes?
      The single quotes are not part of the OP's regex. The OP uses them to store (.*?)\:(.*?) in the scalar $regex1.