繁体   English   中英

关于Perl中文本解析的问题

[英]Question On Text Parsing In Perl

我想这样解析行,

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.我计划将整条线分成四个段,然后解析各个子段。

段1。 S1,F2

段2。 title including several white spaces

段3。 abbr

段4。 single,Here<->There,reply

方法2。我只是编写一个复杂的正则表达式语句来对其进行解析。

哪种方法对我的练习更有意义?

感谢任何评论或建议。

假设输入为指定格式,则可以使用如下正则表达式:

^S(\d+),F(\d+)\s+(.*?)\((.*?)\)\s+(.*?),(.*?),(.*)$

键盘链接

关于第一种方法 ,您可以做的事情就像先用逗号分割字符串一样

my $line =
 'S1,F4  title including several white spaces (abbr) single,Here<->There,reply';
 my ($field1, $field2, $field3, $field4) = split /,/, $line;

然后包含子字符串S1F2 title including several white spaces (abbr) single 的字段上应用正则表达式, 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]

暂无
暂无

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

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