Raku 正则表达式:如何在前瞻中使用捕获组 [英] Raku regex: How to use capturing group inside lookaheads

查看:44
本文介绍了Raku 正则表达式:如何在前瞻中使用捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在先行断言中使用捕获组?

How can I use capturing groups inside lookahead assertion?

此代码:

say "ab" ~~ m/(a) <?before (b) > /;

返回:

「a」
 0 => 「a」

但我希望也能捕获 'b'.

But I was expecting to also capture 'b'.

有没有办法做到这一点?

Is there a way to do so?

我不想将 'b' 留在前瞻之外,因为我不想让 'b' 成为匹配的一部分.

I don't want to leave 'b' outside of the lookahead because I don't want 'b' to be part of the match.

有没有办法捕获b"但仍将其保留在比赛之外?

Is there a way to capture 'b' but still leave it outside of the match?

注意:

我尝试使用 Raku 的捕获标记,例如:

I tried to use Raku's capture markers, as in:

say "ab" ~~ m/<((a))> (b) /;

「a」
 0 => 「a」
 1 => 「b」

但这似乎不像我期望的那样工作,因为即使 'b' 被留在比赛之外,正则表达式已经处理 'b'.而且我也不想被处理.

But this does not seem to work as I expect because even if 'b' is left ouside the match, the regex has processed 'b'. And I don't want to be processed too.

例如:

say 'abab' ~~ m:g/(a)<?before b>|b/;

(「a」
    0 => 「a」
 「b」 
 「a」
    0 => 「a」
 「b」)

# Four matches (what I want)
 

say 'abab' ~~ m:g/<((a))>b|b/;

(「a」
    0 => 「a」 
 「a」
    0 => 「a」)

# Two matches

推荐答案

有没有办法做到这一点?

Is there a way to do so?

不是真的,而是有点.三件事共同阻碍了我们努力实现这一目标.

Not really, but sort of. Three things conspire against us in trying to make this happen.

  1. Raku 正则表达式捕获匹配树的形式.因此 (a(b)) 导致一个位置捕获包含另一个位置捕获.我为什么要提到这个?因为同样的事情发生在 before 之类的东西上,它接受一个正则表达式作为参数:传递给 before 的正则表达式得到它自己的 Match对象.
  2. ? 暗示不捕获".我们可能会考虑去掉它来获取,现在Match对象中确实有一个before键,这听起来很有希望,除了...
  3. before 实际上并不返回它在内部匹配的内容,而是返回一个零宽度的 Match 对象,否则如果我们忘记了 ? 我们最终会发现它不是前瞻.
  1. Raku regex captures form trees of matches. Thus (a(b)) results in one positional capture that contains another positional capture. Why do I mention this? Because the same thing is going on with things like before, which take a regex as an argument: the regex passed to before gets its own Match object.
  2. The ? implies "do not capture". We may think of dropping it to get <before (b)>, and indeed there is a before key in the Match object now, which sounds promising except...
  3. before doesn't actually return what it matched on the inside, but instead a zero-width Match object, otherwise if we did forget the ? we'd end up with it not being a lookahead.

如果我们能从前瞻内部拯救 Match 对象就好了.好吧,我们可以!我们可以声明一个变量,然后将 before 参数正则表达式中的 $/ 绑定到其中:

If only we could rescue the Match object from inside of the lookahead. Well, we can! We can declare a variable and then bind the $/ inside of the before argument regex into it:

say "ab" ~~ m/(a) :my $lookahead; <?before b {$lookahead = $/}> /;
say $lookahead;

给出:

「a」
 0 => 「a」
「b」

这行得通,虽然不幸的是它不像正常捕获那样附加.没有办法做到这一点,尽管我们可以通过 make 附加它:

Which works, although it's unfortunately not attached like a normal capture. There's not a way to do that, although we can attach it via make:

say "ab" ~~ m/(a) :my $lookahead; <?before (b) {$lookahead = $0}> { make $lookahead } /;
say $/.made;

使用相同的输出,除了现在它将可靠地附加到从 m:g 返回的每个匹配对象上,因此即使不美观也很健壮.

With the same output, except now it will be reliably attached to each match object coming back from m:g, and so will be robust, even if not beautiful.

这篇关于Raku 正则表达式:如何在前瞻中使用捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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