[英]Perl regex match longest sequence
我有一个像下面的字符串
atom:link[@me="samiron" and @test1="t1" and @test2="t2"]
我需要一个正则表达式,它将生成以下后向引用
#I would prefer to have
$1 = @test1
$2 = t1
$3 = @test2
$4 = t2
#Or at least. I will break these up in parts later on.
$1 = @test1="t1"
$2 = @test2="t2"
我尝试了类似的东西( and [@\\w]+=["\\w]+)*\\]
,只返回最后一场比赛and @test2="t2"
。完全没有想法。有什么帮助吗?
编辑:实际上@test1="t1"
模式的数量不固定。 正则表达式必须符合这种情况。 Thnx @Pietzcker。
你可以这样做:
my $text = 'atom:link[@me="samiron" and @test1="t1" and @test2="t2"]';
my @results;
while ($text =~ m/and (@\w+)="(\w+)"/g) {
push @results, $1, $2;
}
print Dumper \@results;
结果:
$VAR1 = [
'@me',
'samiron',
'@test1',
't1',
'@test2',
't2'
];
这将为您提供映射“@ test1”=>“t1”的哈希,依此类推:
my %matches = ($str =~ /and (\@\w+)="(\w+)"/g);
说明:/ g全局匹配将为您提供一系列匹配项,如“@ test1”,“t1”,“@ test2”,“t2”,...
当hash%matches分配给此数组时,perl会通过将数组视为键值对自动将数组转换为hash。 因此,哈希%匹配将包含您在漂亮的哈希格式中寻找的内容。
当您使用重复捕获组时,每个新匹配将覆盖任何先前的匹配。
因此,您只能使用正则表达式“查找全部”
@result = $subject =~ m/(?<= and )([@\w]+)=(["\w]+)(?= and |\])/g;
得到所有比赛的数组。
这对我有用:
@result = $s =~ /(@(?!me).*?)="(.*?)"/g;
foreach (@result){
print "$_\n";
}
输出是:
@test1
t1
@test2
t2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.