mkHun mkHun - 2 months ago 8
Perl Question

substitution with eval and repeat the character by grouping string length?

My input as follow

my $s = '<B>Estimated:</B>

The N-terminal of the sequence considered is M (Met).

The estimated half-life is: 30 hours (mammalian reticulocytes, in vitro).
>20 hours (yeast, in vivo).
>10 hours (Escherichia coli, in vivo).


<B>Instability index:</B>

The instability index (II) is computed to be 31.98
This classifies the protein as stable.';


I want to remove the
<B></B>
tags from string and put the underline for bold tags.

I expected output is

Estimated:
---------
The N-terminal of the sequence considered is M (Met).

The estimated half-life is: 30 hours (mammalian reticulocytes, in vitro).
>20 hours (yeast, in vivo).
>10 hours (Escherichia coli, in vivo).


Instability index:
------------------
The instability index (II) is computed to be 31.98
This classifies the protein as stable.


For this tried the following regex but I don't know what is the problem there.

$s=~s/<B>(.+?)<\/B>/"$1\\n";"-" x length($1)/seg; # $1\\n in not working


In the above regex I don't know how to put this
"$1\\n"
? And how to use the continuous statement in substitution separated by
;
or anything else?

How can I fix it?

Answer

The e modifier returns back just the last-executed statement, so

$s=~s/<B>(.+?)<\/B>/"$1\\n";"-" x length($1)/seg;

throws away the "$1\\n" (which should really be "$1\n")

This works:

$s=~s/<B>(.+?)<\/B>/"$1\n" . "-" x length($1)/seg;

The reason I was asking about your Perl version was to assess if it was possible to do what is effectively a variable-length lookbehind with \K:

$s=~s/<B>(.+?)<\/B>\K/ "\n" . "-" x length($1)/seg;

\K is available for Perl versions 5.10+.

Comments