管道和引号的Perl或python正则表达式拆分

时间:2012-05-09 03:30:48

标签: regex perl split pipe

我正在尝试解析字符串 -

输入:"1"|"abc xyz"||"a|25|30"|2345

输出:

"1"
"abc xyz"

"a|25|30"
2345

我该怎么做?

3 个答案:

答案 0 :(得分:5)

#!perl
use strict;
use warnings;
use Text::CSV;

my $in = q{"1"|"abc xyz"||"a|25|30"|2345};
my $csv = Text::CSV->new({ sep_char => '|', quote_char => '"' });
$csv->parse($in);
print $_, "\n" for $csv->fields;

输出:

1
abc xyz

a|25|30
2345

答案 1 :(得分:1)

CSV解析器会删除引号。这些解决方案使它们保持完整。

use Text::ParseWords qw(quotewords);
quotewords(qr'[|]', qr("), q("1"|"abc xyz"||"a|25|30"|2345));
# (
#     '"1"',
#     '"abc xyz"',
#     '',
#     '"a|25|30"',
#     2345
# )

use Data::Record qw();
use Regexp::Common qw(delimited);
my $r = Data::Record->new({ split => qr'[|]', unless => $RE{quoted} });
$r->records(q("1"|"abc xyz"||"a|25|30"|2345));
# (
#     '"1"',
#     '"abc xyz"',
#     '',
#     '"a|25|30"',
#     2345
# )

答案 2 :(得分:0)

use strict;
use warnings;

my $input = q{"1"|"abc xyz"||"a|25|30"|2345||"2"|"z|6|7"|"jkl"||b};
print $& eq '||' ? "\n" : "$&\n" while $input =~ /".*?"|\w+|\|.{0}\|/g;

<强>输出

"1"
"abc xyz"

"a|25|30"
2345

"2"
"z|6|7"
"jkl"

b