作者lgen7604 ()
看板Perl
标题Re: [问题] 请问如何在{}里面使用\1?
时间Tue Jun 18 23:46:44 2013
根据你想做的内容我想到的作法如下
my $line = 'C+10AAAAAAAAAAT-3CGCA';
$line =~ s/((\d+)[ACGTNacgtn]+)/substr($1, length($2)+$2)/eg;
使用到的观念包含:
length function
substr function
substitute with the e modifier
关於 e modifier 的参考资料
http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators
※ 引述《CindyLinz (Cindy Wang)》之铭言:
: ※ 引述《scwg ( )》之铭言:
: : 根据 http://perl5.git.perl.org/perl.git/blob?f=dquote_static.c
: : S_regcurly() 在 parse 的时候 { 之後一定要接数字 -- 不能写 {\1}
: : 我会这样写
: : $line='C1tt+10AAAAAAAAAAT-3CGCA';
: : while ($line =~ /\d+/) {
: : $pre = $`;
: : ($post = $') =~ s/^[acgtn]{$&}//i;
: : $line = $pre . $post;
: : }
: : print $line . $/;
: : --
: : ◆ From: 128.36.232.45
: : 推 herculus6502:我记得可以用eval的option,有请高人出手… 06/17 16:51
: : 推 CindyLinz:翻 perl 的 source 出来解释会不会太恐怖....|| 06/17 18:07
: 查 perlre 可以看到 (??{ CODE }) 的用法,
: 被标示为 experimental, 说是以後可能会无预警改掉.
: 不过它挂在那边不动好像也很久了..
: 不过用的话仍请自负风险 :p
: 你想要的功能可以写成这样:
: $line=~s/(\d+)(??{"[ACGTNacgtn]{$1}"})//g
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 180.177.15.219
1F:推 CindyLinz:这个不错 :p 06/19 17:27
2F:推 YMLS:这个方法很赞喔! 07/18 21:59