Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
I have a string like this,
$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>';

I need to delete <x> tag having the condition is
First <x> tag after <chapnum>1</chapnum> with <chapnum>1A</chapnum> tags
that is
<x><chapter><chapnum>1</chapnum></chapter><x><chapter><chapnum>1A</cha +pnum>
</code>
to be
<x><chapter><chapnum>1</chapnum></chapter><chapter><chapnum>1A</chapnu +m>
</code>
i don't want to remove the last
<x><chapter><chapnum>1A</chapnum>
my output should look like this
<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>

I have tried like this,
$s1='<x><chapter>\s*<chapnum>1</chapnum>'; $s2='<chapter>\s*<chapnum>1A</chapnum>'; $str1 =~s#($s1)(.*?)<x>?=($s2)#$1$2$3#gsi;
Thanks in advance

Replies are listed 'Best First'.
Re: Pattern matching problem
by mantadin (Beadle) on Apr 25, 2006 at 09:32 UTC
    maybe this is what you need:
    my $cn = qr/chapnum/; my $cp = qr/chapter/; $str1 =~ s#(1</$cn>\s*</$cp>\s*)<x>(<$cp>\s*<$cn>1A)#$1$2#s;
    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Pattern matching problem
by prasadbabu (Prior) on Apr 25, 2006 at 09:16 UTC

    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

      I Saw your output that missed out first closing '</chapter>'
      Anyway thanks for your help