我有这样一句话:
A2 IDENT = 'THING1','THING2'
A3 IDENT = 'G'
A4 IDENT = 'SOME THING','ANOTHER THING'
我正试图获得这样的令牌:
A2,IDENT,=,'THING1','THING2'
A3,IDENT,=,'G'
A4,IDENT,=,'SOME THING','ANOTHER THING'
我可以在\s+
然后在,
上进行拆分(以拆分最后一部分),但最后一节中的空格搞乱了。
答案 0 :(得分:4)
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dump;
use Text::ParseWords;
while (<DATA>) {
chomp;
my @words = parse_line( qr{\s+}, 1, $_ );
dd(@words);
}
__DATA__
A2 IDENT = 'THING1','THING2'
A3 IDENT = 'G'
A4 IDENT = 'SOME THING','ANOTHER THING'
输出:
("A2", "IDENT", "=", "'THING1','THING2'")
("A3", "IDENT", "=", "'G'")
("A4", "IDENT", "=", "'SOME THING','ANOTHER THING'")
注意:使用Data::Dump可以更轻松地查看结果数组包含的内容。
答案 1 :(得分:2)
use strict;
use warnings;
for my $line (<DATA>) {
my @fields = $line =~ /('.*?'|\S+),?/g;
print join(',', @fields), "\n";
}
__DATA__
A2 IDENT = 'THING1','THING2'
A3 IDENT = 'G'
A4 IDENT = 'SOME THING','ANOTHER THING'
答案 2 :(得分:1)
(?:\s+|,)(?=(?:[^']*'[^']*')*[^']*$)
您可以按此拆分并稍后删除empty splits
。请参阅演示。