我希望如下所述解析文件。 我想選擇以H開頭的行,直到完成//
的行。 我寫了這個代碼,但它不工作,返回1:在perl中解析文件的正則表達式
use strict;
use warnings;
my $nom='';
# Declare and initialize variables
my $annotation = '';
my $dna = '';
my $record = '';
my $filename = 'ess.txt';
my $i=0;
my $save_input_separator = $/;
open(GBFILE, $filename) or die "Error";
# Set input separator to "//\n" and read in a record to a
#scalar
{
$/="//\n";
$record = <GBFILE>;
}
$/ = $save_input_separator;
$nom=($record=~ /(.*)\/\/\n/s);
print $nom;
exit;
這是輸入:
H ANDN920101
D alpha-CH chemical shifts (Andersen et al., 1992)
R LIT:1810048b PMID:1575719
A Andersen, N.H., Cao, B. and Chen, C.
T Peptide/protein structure analysis using the chemical shift index method:
upfield alpha-CH values reveal dynamic helices and aL sites
J Biochem. and Biophys. Res. Comm. 184, 1008-1014 (1992)
C BUNA790102 0.949
I A/L R/K N/M D/F C/P Q/S E/T G/W H/Y I/V
4.35 4.38 4.75 4.76 4.65 4.37 4.29 3.97 4.63 3.95
4.17 4.36 4.52 4.66 4.44 4.50 4.35 4.70 4.60 3.95
//
你究竟是什麼意思的解析?你想存儲哪些字段?你想如何存儲它?你真的只想解析一個記錄,這是你現在做的事情嗎?這裏有很多問題,我甚至不知道從哪裏開始。 – zb226
是因爲''''後面沒有換行符,但你的代碼期望它? –
而不是'$ save_input_separator',你可以在你的閉包中使用'local $/= ....'來獲得相同的結果。 – Sobrique