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

Hi, I want to search paragraphs tagged <FN1>...</FN1> and place it in a place instead of the indicator <FN1REF>. Generally <FN1>...</FN1> appears at the end of the file and <FN1REF> appears somewhere in the middle of the file. When I use the pattern:

$_ =~ /<FN1>([^<].+)<\/FN1>/;
$_ =~ s/<FN1ref>/<FN1>$1<\/FN1>/g;

Only <FN1></FN1> is placed in the place of the indicator. $1 does not seem to work in the replacement. Can anyone help with the correct syntax?

Replies are listed 'Best First'.
Re: Find and replace using regex
by shmem (Chancellor) on Jul 13, 2006 at 13:30 UTC
    $1 does not seem to work in the replacement.
    because $1 from the m// is reset by the subsequent s///. You have to capture $1 in some variable:
    $_ =~ /<FN1>([^<].+)<\/FN1>/; $found = $1; $_ =~ s/<FN1ref>/<FN1>$found<\/FN1>/g;

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Find and replace using regex
by Samy_rio (Vicar) on Jul 13, 2006 at 12:50 UTC

    Hi rsriram, Try this,

    use strict; use warnings; my $data = do{local $/; <DATA>}; my $i = 1; while ($data =~ s/(<FN$i>(?:(?:(?!<\/FN$i>).)*)<\/FN$i>)\n*//gsi){ my $foot = $1; $data=~ s/<FN${i}ref>/$foot/gi; $i++; } print $data; __DATA__ This is my text<FN1ref>. This is my text This is my text. This is my text<FN2ref>. This is my text This is my text. This is my text<FN3ref>, this is my text This is my text. This is my text<FN4ref>, this is my text This is my text. This is my text<FN5ref>, this is my text This is my text. <FN1>Footnote1</FN1> <FN2>Footnote2</FN2> <FN3>Footnote3</FN3> <FN4>Footnote4</FN4> <FN5>Footnote5</FN5> Output: ------- This is my text<FN1>Footnote1</FN1>. This is my text This is my text. This is my text<FN2>Footnote2</FN2>. This is my text This is my text. This is my text<FN3>Footnote3</FN3>, this is my text This is my text. This is my text<FN4>Footnote4</FN4>, this is my text This is my text. This is my text<FN5>Footnote5</FN5>, this is my text This is my text.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Find and replace using regex
by prasadbabu (Prior) on Jul 13, 2006 at 12:48 UTC

    Hi rsriram,

    Here is one way to do it.

    use strict; use warnings; my $str = do { local $/, <DATA> }; my @fnote = qw(<FN1>footnote1</FN1> <FN2>footnote1</FN2> <FN3>footnote +1</FN3>); #Already retrived footnotes my %hash; for (@fnote) { $hash{$1} = $_ if ($_ =~ /<(FN\d+)>/); } $str =~ s|<(FN\d+)ref>|$hash{$1}|g; print $str; __DATA__ <p> Here some text <FN1ref> and some text Here some text <FN2ref> and some text Here some text <FN3ref> and some text </p> <FN1>afsdfsaf</FN1> <FN2>afsdfsaf</FN2> <FN3>afsdfsaf</FN3> outputs: -------- <p> Here some text <FN1>footnote1</FN1> and some text Here some text <FN2>footnote1</FN2> and some text Here some text <FN3>footnote1</FN3> and some text </p> <FN1>afsdfsaf</FN1> <FN2>afsdfsaf</FN2> <FN3>afsdfsaf</FN3>

    * My 200th post :)

    Prasad

Re: Find and replace using regex
by sgifford (Prior) on Jul 13, 2006 at 16:39 UTC
    Also, if what you're doing is parsing an XML file, you should consider using a proper XML parser, which will handle properly all the strange things that can show up in an XML file.