Regex perl中的模式匹配

Regex perl中的模式匹配,regex,perl,Regex,Perl,我想捕获Name:和之间的单词,Region,它出现在整行中的任何位置。主要的漏洞是名称可以是任何格式 my $line = "Name:Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE,"; my $name = ""; @name = ( $line =~ m/Name:([\w\s\_\,/g ); foreach (@name) {

我想捕获
Name:
之间的单词,Region
,它出现在整行中的任何位置。主要的漏洞是名称可以是任何格式

my $line = "Name:Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE,";
my $name = "";

@name = ( $line =~ m/Name:([\w\s\_\,/g );
foreach (@name) {
   print $name."\n";
}
我需要一个帮助来捕捉这样一个模式,每次它发生在生产线上。因此,对于我提供的行,输出应该是

Amanda_Marry_Rose
Amanda.Marry.Rose
Amanda Marry Rose
Amanda/Marry/Rose
有人知道怎么做吗?我试着保持下面的行,但它给了我错误的输出

Amanda_Marry_Rose
Raghav.S.Thomas
已更正的注册号:

Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE

要在
名称:
和第一个逗号之间捕获,请使用:

这表示要匹配
名称:
后面的一个或多个非逗号字符:

/Name:([^,]+)/g
这比不贪婪的方法更有效,例如:


因为它不需要。

这里有逗号分隔的数据。您应该如何解析它在很大程度上取决于您的数据。如果是成熟的csv数据,最安全的方法是使用适当的csv解析器,例如。如果它是不太严格的数据,那么您可以不用使用轻量级解析器,它还可以作为Perl5中的核心模块。如果您在这里看到的是用户输入的基本字段,那么我建议使用
split
——因为当您知道分隔符时,定义它比定义它里面的所有内容更容易、更安全

/Name:(.+?),/g
除了使用
Text::CSV
的选项外,这些选项中的每一个都提供了相同的输出,该选项还发出了一个未定义的警告,这是非常正确的,因为您的数据后面有一个逗号(表示末尾有一个空字段)

每一个都有不同的优势和劣势
Text::CSV
可能会阻塞不符合CSV格式的数据,
split
无法处理嵌入的逗号,例如
Name:“Doe,John”,…


我们用来提取名称的正则表达式非常简单,它只捕获以
Name:
开头的所有其余行。这还允许您对字段名执行健全性检查,例如,如果突然发现名为
Doe的字段,则发出警告;名称:

简单的方法是在字符串中
名称:
的每个实例之后查找所有非逗号字符序列

use strict;
use warnings;
use Data::Dumper;

my $line = "Name:Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE,";

# Simple split
my @fields = split /,/, $line;
print Dumper for map /^Name:(.*)/, @fields;

use Text::ParseWords;
print Dumper map /^Name:(.*)/, quotewords(',', 0, $line);

use Text::CSV;
my $csv = Text::CSV->new({
        binary => 1,
    });
$csv->parse($line);
print Dumper map /^Name:(.*)/, $csv->fields;
输出

use strict;
use warnings;

my $line = 'Name:Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE,';

my @names = $line =~ /Name:([^,]+)/g;

print "$_\n" for @names;
use strict;
use warnings;

my $line = 'Name:Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE,';

my %info;
my @persons;
while ( $line =~ / ([a-z]+) : ([^:,]+) /gix ) {

   my ($key, $val) = (lc $1, $2);

   if ($info{$key}) {
     push @persons, { %info };
     %info = ();
   }

   $info{$key} = $val;
}
push @persons, { %info };

use Data::Dump;
dd \@persons;

print "\nNames:\n";
print "$_\n" for map $_->{name}, @persons;
但是,将数据解析为哈希数组以便将相关字段聚集在一起可能很有用

Amanda_Marry_Rose
Raghav.S.Thomas
输出

use strict;
use warnings;

my $line = 'Name:Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE,';

my @names = $line =~ /Name:([^,]+)/g;

print "$_\n" for @names;
use strict;
use warnings;

my $line = 'Name:Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE,';

my %info;
my @persons;
while ( $line =~ / ([a-z]+) : ([^:,]+) /gix ) {

   my ($key, $val) = (lc $1, $2);

   if ($info{$key}) {
     push @persons, { %info };
     %info = ();
   }

   $info{$key} = $val;
}
push @persons, { %info };

use Data::Dump;
dd \@persons;

print "\nNames:\n";
print "$_\n" for map $_->{name}, @persons;

您的第一个regexp有不匹配的圆括号和方括号。这是很多反斜杠。其中大多数是多余的。例如,您不需要转义
!“#%&',:;{}
*+?()|@^
不需要在字符类括号内转义
[…]
use strict;
use warnings;

my $line = 'Name:Amanda_Marry_Rose,Region:US,host:USE,cardType:DebitCard,product:Satin,Name:Raghav.S.Thomas,Region:UAE,';

my %info;
my @persons;
while ( $line =~ / ([a-z]+) : ([^:,]+) /gix ) {

   my ($key, $val) = (lc $1, $2);

   if ($info{$key}) {
     push @persons, { %info };
     %info = ();
   }

   $info{$key} = $val;
}
push @persons, { %info };

use Data::Dump;
dd \@persons;

print "\nNames:\n";
print "$_\n" for map $_->{name}, @persons;
[
  {
    cardtype => "DebitCard",
    host     => "USE",
    name     => "Amanda_Marry_Rose",
    product  => "Satin",
    region   => "US",
  },
  {
    name   => "Raghav.S.Thomas",
    region => "UAE",
  },
]

Names:
Amanda_Marry_Rose
Raghav.S.Thomas