in reply to Re: Match, Capture and get position of multiple patterns in the same string
in thread Match, Capture and get position of multiple patterns in the same string
Firstly, thanks to EVERYONE who has commented here. It's been a great help. I posted this before I went to bed and when I get to work this morning, you guys had solved all my problems! Wish it could be the same every day!
I'll certainly check out the perlvar link as Fletch and moritz suggest. I always like a good gem!
Kennethk, you know I tried a while loop cos I thought it should work but it kept hanging. I've since realised that this was down to doing something else in the loop that I didn't tell you guys about as it's a bit silly ;) . Basically I have the need to substitute the match within the string with a lowercase version of itself ie once the matches, positions etc are found the string will then go:
From this: CATINTHEHATWITHABAT To this: catINTHEhatWITHAbat
I was doing this simply as follows (adapted from the code moritz provided - thanks!):
while ($string =~ /($regex)/pg){ my $match = ${^MATCH}; my $start = $-[0]; my $end = $+[0]; my $hitpos = "$start-$end"; my $lcmatch = lc($match); $string =~ s/$match/$lcmatch/g; push @matches, "$match found at $hitpos "; }
However the substitution line seems to cause it to hang and I can't get my head around why as it should just be operating on the current match of which there are only 3 in this instance
Can anyone make any suggestions other than storing each match string in the loop and doing the substitution separately outside the loop.
Again, thanks for the help
Regards, Richard
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Match, Capture and get position of multiple patterns in the same string
by moritz (Cardinal) on Nov 13, 2009 at 07:42 UTC | |
by richardwfrancis (Beadle) on Nov 13, 2009 at 08:33 UTC | |
by moritz (Cardinal) on Nov 13, 2009 at 08:57 UTC | |
|
Re^3: Match, Capture and get position of multiple patterns in the same string
by kennethk (Abbot) on Nov 13, 2009 at 16:03 UTC | |
by richardwfrancis (Beadle) on Nov 16, 2009 at 06:56 UTC |