Re: [問題] 請問如何在{}裡面使用\1?

看板Perl作者時間12年前 (2013/06/18 23:46), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串4/4 (看更多)
根據你想做的內容我想到的作法如下 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

06/19 17:27, , 1F
這個不錯 :p
06/19 17:27, 1F

07/18 21:59, , 2F
這個方法很讚喔!
07/18 21:59, 2F
文章代碼(AID): #1Hm83eGF (Perl)
文章代碼(AID): #1Hm83eGF (Perl)