相关疑难解决方法(0)

将正则表达式替换作为Perl中的变量传递?

我需要将正则表达式替换作为变量传递:

sub proc {
    my $pattern = shift;
    my $txt = "foo baz";

    $txt =~ $pattern;
}

my $pattern = 's/foo/bar/';
proc($pattern);
Run Code Online (Sandbox Code Playgroud)

当然,这不起作用.我试着评估替换:

eval("$txt =~ $pattern;");
Run Code Online (Sandbox Code Playgroud)

但那也不起作用.我在这里错过了多么可怕的事情?

regex perl

23
推荐指数
5
解决办法
3万
查看次数

标签 统计

perl ×1

regex ×1