perl:无法匹配)正则表达式错误

时间:2013-05-02 23:12:14

标签: regex perl

我不是perl程序员,所以我只需要运行这个简单的脚本:

perl -e 'open(FILE,"tmp.plot"); my $seqLength = 643292; my $count=1; while(my $ln = <FILE>){ if( $ln =~ m/^(\d+)\s+(\d+)/ ) { if($1 > $count) { for($i = $count; $i < $1
; $i++){ print "0\n" } }; print "$2\n"; $count=$1+1;   }  } for($i = $count; $i <= $seqLength; $i++){ print "0\n" }' > dnaplotter.plot

错误是: Unmatched ) in regex; marked by <-- HERE in m/^(\d+)\s+(\d+) <-- HERE / at -e line 1.

任何人都知道如何修复它?

提前谢谢!

TP

2 个答案:

答案 0 :(得分:4)

您可能会粘贴由终端软件解释的字符,隐藏您实际运行的命令。

例如,

$ echo -e 'm/^(\\d+)\\s+(\\d+)\x08)/' | od -c
0000000   m   /   ^   (   \   d   +   )   \   s   +   (   \   d   +   )
0000020  \b   )   /  \n
0000024

# Note the extra Backspace and ")" in the od output.

$ echo -e 'm/^(\\d+)\\s+(\\d+)\x08)/'
m/^(\d+)\s+(\d+)/

$ echo -e 'm/^(\\d+)\\s+(\\d+)\x08)/' | perl -c
Unmatched ) in regex; marked by <-- HERE in m/^(\d+)\s+(\d+) <-- HERE / at - line 1.

答案 1 :(得分:1)

这个程序看起来更好地作为脚本布局。使用use strictuse warnings就可以了:

use strict;
use warnings;

open(FILE, "tmp.plot") or die $!;

my $seqLength = 643292;
my $count     = 1;

while (my $ln = <FILE>) {
    if ($ln =~ m/^(\d+)\s+(\d+)/) {
        if ($1 > $count) {
            for (my $i = $count; $i < $1; $i++) {
                print "0\n";
            }
        }
        print "$2\n";
        $count = $1 + 1;
    }
}

for (my $i = $count; $i <= $seqLength; $i++) {
    print "0\n";
}

将其作为

运行
perl script.pl > dnaplotter.plot