2011-11-16 113 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;