简体   繁体   English

Perl正则表达式匹配最长序列

[英]Perl regex match longest sequence

I have a string like below 我有一个像下面的字符串

atom:link[@me="samiron" and @test1="t1" and @test2="t2"]

and I need a regular expression which will generate the following back references 我需要一个正则表达式,它将生成以下后向引用

#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"

I've tried something like ( and [@\\w]+=["\\w]+)*\\] which returns only last match and @test2="t2" . Completely out of ideas. Any help? 我尝试了类似的东西( and [@\\w]+=["\\w]+)*\\] ,只返回最后一场比赛and @test2="t2" 。完全没有想法。有什么帮助吗?

Edit: actually the number of @test1="t1" pattern is not fixed. 编辑:实际上@test1="t1"模式的数量不固定。 And the regex must fit the situation. 正则表达式必须符合这种情况。 Thnx @Pietzcker. Thnx @Pietzcker。

You can do it like this: 你可以这样做:

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;

Result: 结果:

$VAR1 = [
          '@me',
          'samiron',
          '@test1',
          't1',
          '@test2',
          't2'
        ];

This will give you hash which maps "@test1" => "t1" and so on: 这将为您提供映射“@ test1”=>“t1”的哈希,依此类推:

my %matches = ($str =~ /and (\@\w+)="(\w+)"/g);

Explanation: /g global match will give you an array of matches like "@test1", "t1", "@test2", "t2", ... 说明:/ g全局匹配将为您提供一系列匹配项,如“@ test1”,“t1”,“@ test2”,“t2”,...

When hash %matches is assigned to this array, perl will automatically convert array to hash by treating it as key-value pairs. 当hash%matches分配给此数组时,perl会通过将数组视为键值对自动将数组转换为hash。 As a result, hash %matches will contain what are you looking for in nice hash format. 因此,哈希%匹配将包含您在漂亮的哈希格式中寻找的内容。

When you use a repeating capturing group, each new match will overwrite any previous match. 当您使用重复捕获组时,每个新匹配将覆盖任何先前的匹配。

So you can only do a "find all" with a regex like 因此,您只能使用正则表达式“查找全部”

@result = $subject =~ m/(?<= and )([@\w]+)=(["\w]+)(?= and |\])/g;

to get an array of all matches. 得到所有比赛的数组。

This works for me: 这对我有用:

@result = $s =~ /(@(?!me).*?)="(.*?)"/g;
foreach (@result){
    print "$_\n";
}

The output is: 输出是:

@test1
t1
@test2
t2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM