in reply to Help required inText manipulation

Could you please remove the duplicate texts in readmore tags?

First of all there are many perl modules that do the XML parsing for you. Ok, you already are nearly finished, but next time you have to parse XML, you could use something like XML::Parser XML::Rules XML::Bare XML::Smart XML::Easy ...

To make regexes easier to read (and consequently debug) you could add an 'x' modifier and add spaces and line breaks to your regexes (remember to escape your spaces then). Also if you have to match literal '/' characters, using a different delimiter for the regex is recommended. Example:

$bBlkTx =~ s/<BlockAmendment [^>]+><P2>(.*?)<\/P2><\/Block Amendment>/ +\n<lq><P2>$1<\/P2>\n<\/lq>/gi; # versus $bBlkTx =~ s{ <BlockAmendment\ [^>]+ ><P2> (.*?) </P2></Block\ Ame +ndment> } { \n<lq><P2> $1 <\/P2>\n<\/lq> }xgi;

Notice that I highlighted the parts where the substitution happens through spaces. You could even extract part of the regexes into variables with meaningful names and make it even more obvious what you are parsing (though the positive effect in this example is rather small or even nonexistant):

$P2=qr'\s*<P2>\s*'xgi; $AmendmentPrefix= qr'BlockAmendment\ [^>]+ >'xgi; $AmendmentSuffix= qr'</P2></Block\ Amendment>'xgi; $bBlkTx =~ s{ $AmendmentPrefix $P2 (.*?) $AmendmentSuffix } { \n<lq><P2> $1 <\/P2>\n<\/lq> }xgi;

I know, this doesn't help you directly, but it helps you detect errors yourself and if you post more readable code, more people will make the effort to read your code

Do you know which regex is not working? If you don't know, just put print statements in your code that show you the data as it changes, or print the return values from the regexes, they tell you how many substitutions they made

If you know, make sure you add \s* patterns where line breaks or spaces occur

Replies are listed 'Best First'.
Re^2: Help required inText manipulation
by thirilog (Acolyte) on Apr 12, 2011 at 12:12 UTC

    Thanks jethro,

    Will update them.

    The troubling regex is $xmlBody =~ s/$CbBlkTx/$bBlkTx/xgi; within the while.

    just trying to replace back the replaced result '$bBlkTx' in "$xmlBody" but its not perfoming.

    I may be wrong in trying this please add some advice in this.

    #Processing BlockAmendments while ($xmlBody =~ m/(<BlockAmendment [^>]*>(.+?)<\/BlockAmendment +>)/gi){ $CbBlkTx = $1; $bBlkTx = $CbBlkTx; if ($bBlkTx =~ m/(<BlockAmendment [^>]+><P2>(.*?)<\/P2><\/Bloc +kAmendment>)/){ $bBlkTx =~ s/<BlockAmendment [^>]+><P2>(.*?)<\/P2><\/Block +Amendment>/\n<lq><P2>$1<\/P2>\n<\/lq>/xgi; $bBlkTx =~ s/<P2><Pnumber>([^<]+)<\/Pnumber><P2para><Text> +(.+?)<\/Text>(.*?)<\/P2para><\/P2>/\n<s1><no>($1)<\/no>\n<pt>$2<\/pt> +$3\n<\/s1>/xgi; $bBlkTx =~ s/<P3><Pnumber>([^<]+)<\/Pnumber><P3para><Text> +(.+?)<\/Text>(.*?)<\/P3para><\/P3>/\n<s2><no>($1)<\/no>\n<pt>$2<\/pt> +$3\n<\/s2>/xgi; $bBlkTx =~ s/<P4><Pnumber>([^<]+)<\/Pnumber><P4para><Text> +(.+?)<\/Text>(.*?)<\/P4para><\/P4>/\n<s3><no>($1)<\/no>\n<pt>$2<\/pt> +$3\n<\/s3>/xgi; $bBlkTx =~ s/<P5><Pnumber>([^<]+)<\/Pnumber><P5para><Text> +(.+?)<\/Text>(.*?)<\/P5para><\/P5>/\n<s4><no>($1)<\/no>\n<pt>$2<\/pt> +$3\n<\/s4>/xgi; } $bBlkTx =~ s/<\/pt>\n<\/s([0-9])><Text>(.+?)<\/Text>/ $2<\/pt> +<\/s$1>/xgi; print "$CbBlkTx\n\n=====>\n$bBlkTx\n\n"; $xmlBody =~ s/$CbBlkTx/$bBlkTx/gi; }

    thanks

      Ok, usual problem is that some characters in $CbBlkTx are regex special chars. If you use

      $xmlBody =~ s/\Q$CbBlkTx\E/$bBlkTx/xgi;

      instead, all special characters between \Q and \E are escaped. If you want more information try 'perldoc -f quotemeta', quotemeta is the function behind \Q\E

      UPDATE: Added missing word "information". I didn't want to insinuate that quotemeta delivers more escapes than \Q\E ;-)