in reply to find and replace

'just try this
$a = "<exp>2a+&alpha;-b2+b&beta;2</exp><br>"; $a =~ s#(<exp>)(.*?)(<\/exp>)#$1.&expr($2).$3#egsi; print $a; sub expr { my($a) = @_; $a =~ s#([A-Z])#<it>$1</it>#gsi; $a =~ s#(\&)(.*?)(\;)#$1.&tmp($2).$3#egsi; sub tmp { my($tmp) = @_; $tmp =~ s#(<it>|<\/it>)##gsi; return $tmp; } return $a; }

o/p : <exp>2<it>a</it>+α-<it>b</it>2+<it>b</it>β2</exp>

Regards,

Gubendran.L

Replies are listed 'Best First'.
Re^2: find and replace
by texuser74 (Monk) on Dec 29, 2004 at 01:34 UTC
    Thank you very much for all you guys who helped me.

    I came out with a solution some thing like this.

    $infile = $ARGV[0]; open(IN, "<$infile"); open(OUT, ">temp.out"); { local $/ = '<exp>'; print OUT scalar <IN>; for (<IN>) { s@([\d\D]*?</exp>)@ my $var = $1; $var =~ s#([a-z|A-z]+)#<it>$1</it>#g; $var =~ s#<<it>(.*?)</it>>#<$1>#g; $var =~ s#</<it>(.*?)</it>>#</$1>#g; $var =~ s#&(.*?)<it>(.*?)</it>(.*?)\;#&$1$2$3;#g; $var @e; print OUT } } close(IN); close(OUT);
    Thanks, raj