2016-09-07 86 views
2

我输入如下用eval替换并通过分组字符串长度来重复字符?

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.'; 

我想从字符串中删除的<B></B>标签,并把下划线粗体标签。

我预期的输出是

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. 

对于这种尝试下面的正则表达式,但我不知道是什么问题存在。

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

在上面的正则表达式我不知道如何把这个"$1\n"?以及如何使用由;或其他分隔的替代连续语句?

我该如何解决?

+0

您正在使用哪个版本的Perl? – Zaid

+0

@Zaid Perl版本是'5.14' – mkHun

回答

2

e修改返回刚刚过去执行的语句,所以

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

扔掉的"$1\\n"(这确实应该"$1\n"

这工作:

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

的我询问你的Perl版本的原因是为了评估是否有可能实现有效的可变长度lookbe与\K后腿:

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

\K是可用于Perl版本5.10+。

+0

现在我忘记了连接。这真的很好,谢谢你:) – mkHun

+1

@mkHun不客气。在我的编辑中试试'\ K'建议;它更清洁,因为您不必在替换字符串中使用'$ 1'来替代超过必要的值。 – Zaid

+0

仅供参考:'\ K'是一种*可变长度**正面** lookbehind *解决方法。 –

相关问题