2015-06-11 30 views
1

我希望如下所述解析文件。 我想選擇以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 

// 
+0

你究竟是什麼意思的解析?你想存儲哪些字段?你想如何存儲它?你真的只想解析一個記錄,這是你現在做的事情嗎?這裏有很多問題,我甚至不知道從哪裏開始。 – zb226

+0

是因爲''''後面沒有換行符,但你的代碼期望它? –

+1

而不是'$ save_input_separator',你可以在你的閉包中使用'local $/= ....'來獲得相同的結果。 – Sobrique

回答

0

你爲什麼不乾脆用my $result = ($input =~ /H(.+?)\/\//)

還請記住,數字變量存儲您擁有的最後一次成功匹配。通常是那些在 if ($input =~ /some_regex/)

使用則表達式將計算爲1,如果TRUE 和$ 1,$ 2,...將存儲你的比賽,這就是爲什麼你可能得到1作爲輸出。

0
use strict; 
use warnings; 
my $nom = ''; 


# Declare and initialize variables 

my $record = ''; 
my $filename = 'ess.txt'; 
open(GBFILE, $filename) or die "Error"; 

# Set input separator to "//\n" and read in a record to a 
#scalar 

my @val = <GBFILE>; 

foreach my $val (@val) 
{ 
$val =~ /(^H.*)..(\/\/)/; 
$record .= $val if($val); 
} 
print "Start +++ $record +++ End"; 

exit;