简体   繁体   English

Perl 正则表达式从字符串中提取多个匹配项

[英]Perl regex to extract multiple matches from string

I have a string for example例如我有一个字符串

id:123,createdby:'testuser1',"lastmodifiedby":'testuser2'.....

I want to extract the 2 user names (testuser1, testuser2) and save it to an array.我想提取 2 个用户名(testuser1、testuser2)并将其保存到一个数组中。

You don't need to do everything in one pattern.您不需要以一种模式执行所有操作。 Do something simple in multiple matches:在多场比赛中做一些简单的事情:

my $string = qq(id:123,createdby:'testuser1',"lastmodifiedby":'testuser2');

my( $created_by )       = $string =~ /,createdby:'(.*?)'/;
my( $last_modified_by ) = $string =~ /,"lastmodifiedby":'(.*?)'/;

print <<"HERE";
Created:          $created_by
Last modified by: $last_modified_by
HERE

But, this looks like comma-separated data, and the data that you show are inconsistently quoted.但是,这看起来像是逗号分隔的数据,并且您显示的数据引用不一致。 I don't know if that's from you typing it out or it's your actual data.我不知道那是你输入的还是你的实际数据。

But, it also looks like it might have come from JSON.但是,它看起来也可能来自 JSON。 It that's true, there are much better ways to extract data.没错,有更好的方法来提取数据。

Try this试试这个

use strict;
use warnings;

my $string = q[id:123,createdby:'testuser1',"lastmodifiedby":'testuser2'....];

my @matches = ($string =~ /,createdby:'(.+?)',"lastmodifiedby":'(.+?)'/) ;

print " @matches\n";

Outputs产出

testuser1 testuser2

User requirements changed to allow coping with missing files.用户要求更改为允许处理丢失的文件。 To deal with that, try this要解决这个问题,试试这个

use strict;
use warnings;

my $string1 = q[id:123,createdby:'testuser1',"lastmodifiedby":'testuser2'....];
my $string2 = q[id:123,createdby:'testuser1'....] ;

for my $s ($string1, $string2)
{
    my @matches = ( $s =~ /(?:createdby|"lastmodifiedby"):'(.+?)'/g ) ;
    print "@matches\n";
}

Outputs产出

testuser1 testuser2
testuser1

Problem description does not give enough details, inside the string quoting is not consistent.问题描述不够详细,里面的字符串引用不一致。

As already stated the string can be part of JSON block and in such case should be handled by other means.如前所述,字符串可以是 JSON 块的一部分,在这种情况下应通过其他方式处理。 Perhaps this assumption is correct but it not clearly stated in the question.也许这个假设是正确的,但问题中没有明确说明。

Please read How do I ask a good question?请阅读我如何提出一个好问题? , How to create a Minimal, Reproducible Example . ,如何创建最小的、可重现的示例

Otherwise assumed that quoting is just a typing error.否则假设引用只是一个打字错误。 A bigger data sample and better problem description would be a significant improvement of the question.更大的数据样本和更好的问题描述将是问题的重大改进。

Following code sample demonstrates one of possible approaches to get desired result and assumes that data fields does not includes , and : (otherwise other approach to process data must be in place).以下代码示例演示了获得所需结果的一种可能方法,并假设数据字段不包括,: (否则必须采用其他处理数据的方法)。

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my($str,%data,@arr);

$str = "id:123,createdby:'testuser1','lastmodifiedby':'testuser2'";
$str =~ s/'//g;
%data = split(/[:,]/,$str);

say Dumper(\%data);

@arr = ($data{createdby},$data{lastmodifiedby});

say Dumper(\@arr);

Output输出

$VAR1 = {
          'id' => '123',
          'createdby' => 'testuser1',
          'lastmodifiedby' => 'testuser2'
        };

$VAR1 = [
          'testuser1',
          'testuser2'
        ];

Other approach could be as following其他方法可能如下

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my($str,$re,@data,@arr);

$str  = "id:123,createdby:'testuser1',\"lastmodifiedby\":'testuser2'";
@data = split(',',$str);
$re   = qr/(createdby|lastmodifiedby)/;

for ( @data ) {
    next unless /$re/;
    s/['"]//g;
    my($k,$v) = split(':',$_);
    push @arr, $v;
}

say Dumper(\@arr);

Output输出

$VAR1 = [
          'testuser1',
          'testuser2'
        ];

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

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