2011-11-16 110 views
1

我想在一個字符串中搜索一個特定模式,但具有特定長度(最大長度爲20)。 例子:Perl正則表達式模式長度

字符串:

hellokkkkkkkkkkhellokhellokkhellokkkkk 

正則表達式:

/(hello.*?hello.*?hello)/ 

,但它給了我下面的模式

LOC:0至26

hellokkkkkkkkkkhellokhello 

但我想要我只有第二種模式(意味着hellokhellokkhello),其長度爲< 20 .. 有什麼建議嗎?

+0

@TLP:對不起,轉義字符放在我不知道爲什麼:-( – Toto

+0

@ M42好吧,這只是看上去有些詭異。 – TLP

回答

4

要獲得重疊匹配,請使用look-ahead

my $string = 'hellokkkkkkkkkkhellokhellokkhellokkkkk'; 
say for 
    grep { length($_) < 20 } 
    $string =~ /(?=(hello.*?hello.*?hello))/g;