用于在perl中解析文件的正则表达式

时间:2015-06-11 08:06:14

标签: regex perl

我希望解析一个文件,如下所述。 我想选择以H开头的行,直到由//完成的行。 我写了这段代码,但它不起作用并返回1:

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

//

2 个答案:

答案 0 :(得分:0)

为什么不简单地使用my $result = ($input =~ /H(.+?)\/\//)

另请注意,数字变量会存储您上次成功的匹配项。通常用于 if ($input =~ /some_regex/)

如果TRUE,表达式将评估为1 和$ 1,$ 2,...将存储您的匹配,这就是为什么您可能得到1作为输出。

答案 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;