2010-11-16 26 views
0

我想分析該行,這一點,問題文本分析在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. 我打算整行被劃分到四個段然後解析各個子段。

segment1。 S1,F2

分段2。 title including several white spaces

segment3。 abbr

segment4。 single,Here<->There,reply

方法2 我只寫一個複雜的正則表達式語句解析它。

哪種方法更適合我的實踐?

讚賞任何意見或建議。

+1

的可能重複[需要幫助Perl reg ex? ](http://stackoverflow.com/questions/4192213/need-help-with-perl-reg-ex)那是一個在這之後,但是有一個更好的答案是一個更好的問題 - 這是一個確切的基本問題。 – Axeman 2010-11-16 15:09:29

回答

2

假設你的輸入是在指定的,你可以使用正則表達式,如格式:

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

Codepad link

+0

嗨。爲什麼你的表達以'$'(1美元)結束?我測試過,發現它運行良好,沒有'$'。 – 2010-11-16 06:52:32

+2

@Nano HE:'^'匹配字符串的開頭,'$'匹配字符串的結尾...... – Jon 2010-11-16 07:02:33

1

關於你第一種方法,你所能做的就是像第一通過分割字符串逗號,像

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

和th恩場上應用正則表達式方含子S1F2 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]