如何将变量插入 Perl 6 正则表达式? [英] How can I interpolate a variable into a Perl 6 regex?

查看:39
本文介绍了如何将变量插入 Perl 6 正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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:

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.

我可以做额外的工作来关闭一个副本(可能比我显示的更多,这取决于 $n 中的内容),我觉得这很糟糕:

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:

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

但是,我仍然希望有一种方法来最终确定"一个模式(天哪,我只是要求 /o 回来).可能是 Regex 中的一种方法.我认为人们会寻找这个功能.

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.

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

或者,Perl 6 有更好的方法来做这种事情,我只是满脑子都是老派的想法.Perl 6 有规则而不是正则表达式.这一切背后实际上有一个解析器.我认为定义一个 tokenrule 可能是要走的路,但我想我遇到了同样的问题.我看不出有什么子规则工厂.

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.

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

Is there some other way I could do this?

推荐答案

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

As an alternative to using a closure, you can of course build the regex via 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...

这篇关于如何将变量插入 Perl 6 正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆