我想解析这行,
S1,F2 title including several white spaces (abbr) single,Here<->There,reply
我希望输出如下,
1
2
title including several white spaces
abbr
single
Here22There # identify <-> and translate it to 22;
reply
我想知道如何解析上面的那行?
方法1。 我计划将整行拆分为四个段然后解析各个子段。
SEGMENT1。 S1,F2
分段2。 title including several white spaces
段3。 abbr
segment4。 single,Here<->There,reply
方法2。 我只是编写一个复杂的正则表达式语句来解析它。
哪种方法对我的练习更有意义?
对任何意见或建议表示赞赏。
答案 0 :(得分:2)
答案 1 :(得分:1)
关于第一种方法,您可以做的就像首先用逗号分隔字符串,例如
my $line =
'S1,F4 title including several white spaces (abbr) single,Here<->There,reply';
my ($field1, $field2, $field3, $field4) = split /,/, $line;
然后在字段上应用正则表达式包含子字符串S1
和F2 title including several white spaces (abbr) single
my ($field5) = $field1 =~ /S(\d+)/;
my ($field6, $field7, $field8, $field9) =
$field2 =~ m/^F(\d+)\s+(.*?)\((.*?)\)\s+(.*?)$/;
它适用于所有这些字符串,有助于避免使用和制作复杂的正则表达式,
S1,F2 title including several white spaces (abbr) single,Here<->There,reply
S1,F2 title including several white spaces (abbr) single,Here<->There
S1,F2 title including several white spaces (abbr) single,Here<->There,[reply]