in reply to Re: Regex not working
in thread Regex not working

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.

Replies are listed 'Best First'.
Re^3: Regex not working
by graff (Chancellor) on Sep 13, 2011 at 08:35 UTC
    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