in reply to Regex not working

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

Replies are listed 'Best First'.
Re^2: Regex not working
by Perllace (Acolyte) on Sep 13, 2011 at 04:27 UTC
    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.
        Thank you graff and jethro..it works