簡體   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