Perl - 上下文提取(正则表达式)

时间:2014-04-04 15:52:03

标签: regex perl

我似乎无法让我的代码工作。我已经尝试了几种变化,并在论坛后查看论坛,但未能让它工作。帮助赞赏:

my $cpp_file = (passed in by other script)
my $find = "DTX103";
my $makefile_path = "$varA/$varB/Makefile";

open(my $fh,  "<", $makefile_path or die "Could not open Makefile: $!\n";
my @lines = <$fh>;
close $fh;
foreach $line (@lines) {
    if ($line =~ /$find/) && ($line =~ /$cpp_file/) {
        $line =~ m/\s*$find\s*$cpp_file\s*(.+?)\s*/;
        my $cpp_path = $1;

        if (-e "$cpp_path/$cpp_file") {
            print "Path to file taken from Makefile as: $cpp_path\n";
            $cpp_file = "$cpp_path/$cpp_file";

            $find = "true";
            break;
        }
    }
}

请不要批评为什么我会做$cpp_file = "$cpp_path/$cpp_file"这样的事情。这些事情不适合我。

正则表达式的目标是:

        $line =~ m/\s*$find\s*$cpp_file\s*(.+?)\s*/;

0 or more white space 
$find
0 or more white space 
$cpp_file (passed in as actual file name)
0 or more white space 
THIS IS WHAT I NEED
0 or more white space

我也想知道我是否也可以使用:

        $line =~ m/\s*$find\s*$cpp_file\s*($1)\s*/;

由于

0 个答案:

没有答案