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

Hi,

I want to tag only the alphabets found inside <exp> and </exp> with the italic tag <it></it>. I want to skip numbers, symbols and entities. If a number, symbol or entity comes in between the text then <it> tag should close and then open.

example 1

input:<exp>a+b+&beta;</exp><br> output:<exp><it>a</it>+<it>b</it>+&beta;</exp><br>

example 2

input:<exp>2a+&alpha;-b2</exp><br> output:<exp>2<it>a</it>+&alpha;-<it>b</it>2</exp><br>

Replies are listed 'Best First'.
Re: find and replace
by BUU (Prior) on Dec 28, 2004 at 04:26 UTC
    Hi,

    I want you to show us what code you've attempted to write to solve this problem, and why you think your code doesn't work. I'm also interested in seeing which resources you consulted before you decided to ask us. This isn't a "do my homework for me" type site, sorry. We are more than willing to help perl programmers solve perl programming problems, but typically not to write your entire script for you.
Re: find and replace
by gopalr (Priest) on Dec 28, 2004 at 11:50 UTC

    Hi,

    You can try this.

    $input=~s#(<exp>)(.+?)(</exp>)#$1.Replace($2).$3#eg; sub Replace { my ($line)=@_; $line=~s#([a-zA-Z]+)#<it>$1</it>#g; $line=~s#(&[0-9]*)<it>([^;]+)</it>(;)#$1$2$3#g; return $line; }

    Regards,

    Gopal.R

Re: find and replace
by perlsen (Chaplain) on Dec 28, 2004 at 11:58 UTC

    just try this

    $input=':<exp>2aF+-b&amp;2z4676</exp><br>'; $input =~ s/(<exp>)(.*?)(<\/exp>)/$1.&exp($2).$3/egsi; print $input; sub exp { my ($a)=@_; $a =~ s/[a-z]+/<it>$&<\/it>/gsi; $a =~ s/(&)(.*?)(;)/$1.&amp($2).$3/egsi; sub amp { my ($m)=@_; $m =~ s/<\/?it>//gsi; return($m); } return($a); } output: :<exp>2<it>aF</it>+-<it>b</it>&amp;2<it>z</it>4676</exp><br>

    senthil kumar.k

Re: find and replace
by superfrink (Curate) on Dec 28, 2004 at 05:49 UTC
    While not necessarily required I would look into Parse::RecDescent .
    I don't know what else your file has in it but maybe HTML::Parser might also help.
Re: find and replace
by gube (Parson) on Dec 28, 2004 at 14:21 UTC
    '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

      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