如何将变量插入到Perl 6正则表达式中?

时间:2021-04-06 23:21:00

Synopsis 05 mentions that Perl 6 doesn't interpolate variables into a regex, but you can associate an external variable with a pattern. The docs don't mention this feature as far as I can tell. I think people are still going to want to build up a pattern from a string somehow, so I'm curious how that would work.

概要05提到Perl 6不会将变量插入到正则表达式中,但您可以将外部变量与模式相关联。据我所知,文档没有提到这个功能。我认为人们仍然希望以某种方式从字符串中建立一个模式,所以我很好奇这是如何工作的。

Here's a program that demonstrates what happens now. I don't know if that's what is supposed to happen or what anyone intended. I insert a variable into a pattern. If you look at $r with .perl, you see the variable name. Then, I apply the pattern and it matches. I change the variable's value. Now the pattern doesn't match. Change it to something else that would work, and it matches again:

这是一个演示现在发生的事情的程序。我不知道这是应该发生的事情还是任何人的意图。我将变量插入到模式中。如果你用.perl查看$ r,你会看到变量名。然后,我应用模式并匹配。我改变了变量的值。现在模式不匹配。将其更改为可行的其他内容,并再次匹配:

my $target = 'abcdef';

my $n = 'abc';
my $r = rx/ ( <$n> ) /;

# the smart match like this doesn't return a Match object
# https://rt.perl.org/Ticket/Display.html?id=126969
put 'rx// directly: ',
    $target ~~ $r
        ?? "Matched $0" !! 'Misssed';

# now, change $n. The same $r won't match.
$n = 'xyz';

put 'rx// directly: ',
    $target ~~ $r
        ?? "Matched $0" !! 'Misssed';

# now, change back $n. The same $r does match.
$n = 'ab';
put 'rx// directly: ',
    $target ~~ $r
        ?? "Matched $0" !! 'Misssed';

If that's what it's supposed to do, fine. The docs are light here, and the tests (the de facto spec) aren't sophisticated for long range behavior like this.

如果这是它应该做的,那很好。这里的文档很简单,测试(事实上的规范)对于像这样的远程行为并不复杂。

I could do extra work to close over a copy (and maybe more work than I show depending on what is in $n), which I find unperly:

我可以做一些额外的工作来关闭副本(也许比我显示的更多的工作取决于$ n中的内容),我觉得不合适:

my $r = do {
    my $m = $n;
    rx/ <$m> /;
    };

But, I'd still like to have a way to "finalize" a pattern (oh my god, I just asked for /o to come back). A method in Regex perhaps. I think people will look for this feature.

但是,我仍然希望有一种方法来“敲定”一个模式(哦,天哪,我只是要求/ o回来)。也许是Regex中的一种方法。我想人们会寻找这个功能。

my $r = rx/ .... /.finalize;  # I wish!

Or, Perl 6 has a much better way to do this sort of thing and I'm just full of old school thinking. Perl 6 has rules instead of regexes. There's actually a parser behind all this. I thought defining a token or rule might be the way to go, but I think I run into the same problem. I don't see a what to have a subrule factory.

或者,Perl 6有更好的方法来做这种事情,我只是充满了旧学校的想法。 Perl 6有规则而不是正则表达式。这背后实际上有一个解析器。我认为定义一个标记或规则可能是要走的路,但我想我遇到了同样的问题。我没有看到什么有一个subrule工厂。

Is there some other way I could do this?

还有其他方法可以做到这一点吗?

1 个解决方案

#1


4  

As an alternative to using a closure, you can of course build the regex via EVAL.

作为使用闭包的替代方法,您当然可以通过EVAL构建正则表达式。

Besides these two arguably subpar solutions, I'm drawing a blank as well. Note that you can do more complex interpolations via the <{...}> syntax, eg

除了这两个可以说是低于标准的解决方案,我也画了一个空白。请注意,您可以通过<{...}>语法执行更复杂的插值,例如

/ <{ BEGIN compute-string-once-at-compile-time }> /

but I don't see how that can be used to solve the problem...

但我不知道如何用它来解决问题......

#1


4  

As an alternative to using a closure, you can of course build the regex via EVAL.

作为使用闭包的替代方法,您当然可以通过EVAL构建正则表达式。

Besides these two arguably subpar solutions, I'm drawing a blank as well. Note that you can do more complex interpolations via the <{...}> syntax, eg

除了这两个可以说是低于标准的解决方案,我也画了一个空白。请注意,您可以通过<{...}>语法执行更复杂的插值,例如

/ <{ BEGIN compute-string-once-at-compile-time }> /

but I don't see how that can be used to solve the problem...

但我不知道如何用它来解决问题......