in reply to Pattern matching problem
Hi Anonymous Monk, Here is one way to do it. You have done couple of mistakes, you have not used the paranthesis properly for ?=$s2 and for your requirement, 'g' option modifier is not necessary. You have to take a look at perlre.
$str1=' <x><chapter> <chapnum>1</chapnum> </chapter> <x><chapter> <chapnum>1A</chapnum> </chapter> <chapter> <chapnum>1B</chapnum> </chapter> <x><chapter> <chapnum>1</chapnum> </chapter> <chapter> <chapnum>3</chapnum> </chapter> <x><chapter> <chapnum>1A</chapnum> </chapter><x>'; $s1='<x><chapter>\s*<chapnum>1</chapnum>'; $s2='<chapter>\s*<chapnum>1A</chapnum>'; $str1 =~s#($s1)((?:(?!<x>).)*)<x>(?=$s2)#$1$2$3#si; print $str1; output: ------- <x><chapter> <chapnum>1</chapnum> </chapter> <chapter> <chapnum>1A</chapnum> </chapter> <chapter> <chapnum>1B</chapnum> </chapter> <x><chapter> <chapnum>1</chapnum> </chapter> <chapter> <chapnum>3</chapnum> </chapter> <x><chapter> <chapnum>1A</chapnum> </chapter><x>
update: Removed first ?:, now exactly matches your output.
Prasad
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Pattern matching problem
by Anonymous Monk on Apr 25, 2006 at 09:31 UTC |