OM_Zen has asked for the wisdom of the Perl Monks concerning the following question:
my $a = "x|xx|xx|xxxx|xx|xxx|xx|xx|x"; $a =~ s/x\|xx\|x/x\|x x\|x/g; print "[$a]\n"; This gives the output [x|x x|xx|xxxx|x x|xxx|x x|xx|x]
The string above has this pattern x|xx|x ,that has to be changed to x|x x|x,( my intent is to change |xx| alone to |x x| and not any other occurances of xx ) when I am doing this , the part of the code that has the pattern like x|xx|xx|x is changed to x|x x|xx|x, I understand that when the string is parsed and the pattern of x|xx|x occurs , it is changed to x|x x|x, but the next time when it occurs together the parser would have crossed the pattern and hence changes only the first occurance and leaves the second one as it was in the string .
I am asking the way to recursively do pattern matching or doing backtracking or something to match the second pattern as well
Alternately , a colleague had a while loop going
while($_ =~ s/x\|xx\|x/x\|x x\|x/g){ }
20030208 Edit by Corion - changed text from within code to normal text.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A complex recursive regex,help
by tachyon (Chancellor) on Feb 06, 2003 at 00:46 UTC | |
by OM_Zen (Scribe) on Feb 06, 2003 at 18:53 UTC | |
by tachyon (Chancellor) on Feb 06, 2003 at 23:14 UTC | |
by OM_Zen (Scribe) on Feb 06, 2003 at 01:00 UTC | |
by tachyon (Chancellor) on Feb 06, 2003 at 01:07 UTC | |
by OM_Zen (Scribe) on Feb 06, 2003 at 01:12 UTC | |
|
Re: A complex recursive regex,help
by BrowserUk (Patriarch) on Feb 06, 2003 at 00:55 UTC | |
by OM_Zen (Scribe) on Feb 06, 2003 at 01:07 UTC | |
by BrowserUk (Patriarch) on Feb 06, 2003 at 01:44 UTC | |
by OM_Zen (Scribe) on Feb 06, 2003 at 04:45 UTC | |
by BrowserUk (Patriarch) on Feb 06, 2003 at 08:05 UTC | |
|
Re: A complex recursive regex,help
by Enlil (Parson) on Feb 06, 2003 at 00:59 UTC |