Perl,正则表达式,从一行中提取数据

时间:2010-04-09 16:13:21

标签: regex perl path

我试图用perl

提取部分行
use strict; 
use warnings;


# Set path for my.txt and extract datadir
my @myfile = "C:\backups\MySQL\my.txt";
my @datadir = "";

open READMYFILE, @myfile or die "Error, my.txt not found.\n";
    while (<READMYFILE>) {
        # Read file and extract DataDir path
        if (/C:\backups/gi) {
        push @datadir, $_;
        }
    }

# ensure the path was found
print @datadir . " \n";

基本上我首先尝试设置my.txt文件的位置。接下来我试着阅读它并用正则表达式拉出部分线。我得到的错误是:

  

无法识别的逃脱\ m传递   在1130.pl第17行。

我查看了How can I grab multiple lines after a matching line in Perl?以了解如何读取文件并匹配其中的一行,但我不是100%确定我正确地做到这一点或以最佳方式。我似乎也产生错误:

  

错误,找不到my.txt。

但该文件确实存在于文件夹C:\ backups \ MySQL \

6 个答案:

答案 0 :(得分:4)

当Perl看到字符串"C:\backups\MySQL\my.txt"时,它会尝试解析任何转义序列,例如\n。但是当它在\m中看到\my.txt时,它是一个无法识别的转义序列,因此就是错误。

解决此问题的一种方法是正确转义反斜杠:"C:\\backups\\MySQL\\my.txt"。另一种解决方法是使用单引号而不是双引号:'C:\backups\MySQL\my.txt'。然而另一种方式是使用q()构造:q(C:\backups\MySQL\my.txt)

答案 1 :(得分:4)

由于存在一些问题,我会对我在下面的代码中所做的更改发表评论。

use strict; 
use warnings;
# For pretty dumping of arrays and what not.
use Data::Dumper;

# Use single quotes so you don't have to worry about escaping '\'s.
# Use a scalar ($) instead of an array(@) for storing the string.
my $myfile = 'C:\backups\MySQL\my.txt';

# No need to initialize the array.
my @datadir;

# I believe using a scalar is preferred for file handles.
# $! will contain the error if we couldn't open the file.
open(my $readmyfile, $myfile) or die "error opening: $!";

while (<$readmyfile>) {
    # You must escape '\'s by doubling them.
    # If you are just testing to see if the line contains 'c:\backups' you do not
    # need /g for the regex. /g is for repeating matches
    if (/C:\\backups/i) {
        push(@datadir, $_);
    }
}

# Data::Dumper would be better for dumping the array for debugging.
# Dumper wants a reference to the array.
print Dumper(\@datadir);

<强>更新

如果你指的是Data :: Dumper的输出,它只是用于表示数组的漂亮表示。如果您需要特定格式的输出,则必须对其进行编码。一个开始是:

print "$_\n" for (@datadir);

答案 2 :(得分:1)

使用正斜杠代替背斜

答案 3 :(得分:1)

您不应该使用$myfile代替@myfile吗?后者为你提供了一个数组,并且由于你在标量上下文中引用它,它被解除引用(所以它实际上是试图打开一个名为ARRAY(0xdeadbeef)而不是实际文件名的“文件”。)

答案 4 :(得分:1)

找不到文件,因为当数组期望标量时,你正在将数组传递给open,所以我猜这个数组是在标量上下文而不是列表中进行评估所以你'实际上告诉perl尝试打开名为“1”的文件而不是“my.txt”文件。

尝试这样的事情:

my $a = 'filename';
open FH, $a or die "Error, could not open $a: $!";
...

答案 5 :(得分:1)

正如其他人所说,部分问题是使用" "而不是' '类型的引用。 我总是尝试使用' ',除非我知道我需要包含转义或插入变量。 这里有一些陷阱

    use 5.10.0 ;
    use warnings ;

    say "file is c:\mydir" ;
    say "please pay $100 ";
    say "on VMS the system directory is sys$system" ;
    say "see you @5 ";

使用双引号

    Unrecognized escape \m passed through at (eval 1) line 2.
    Possible unintended interpolation of @5 in string at (eval 1) line 5.
    file is c:mydir
    Use of uninitialized value $100 in concatenation (.) or string at (eval 1) line 3.
    please pay
    Use of uninitialized value $system in concatenation (.) or string at (eval 1) line 4.
    on VMS the system directory is sys
    see you

使用单引号

    file is c:\mydir
    please pay $100
    on VMS the system directory is sys$system
    see you @5
相关问题