gpt4 book ai didi

raku - :ex and :ov adverbs with Perl 6 named captures

转载 作者:行者123 更新时间:2023-12-04 16:57:59 25 4
gpt4 key购买 nike

我不完全明白,为什么这里的结果不同。是否:ov仅适用于 <left> ,所以找到最长的匹配它不会做任何其他事情吗?

my regex left {
a | ab
}
my regex right {
bc | c
}

"abc" ~~ m:ex/<left><right>
{put $<left>, '|', $<right>}/; # 'ab|c' and 'a|bc'
say '---';

"abc" ~~ m:ov/<left><right>
{put $<left>, '|', $<right>}/; # only 'ab|c'

最佳答案

:ex将找到重叠匹配的所有可能组合。
:ov就像 :ex除了它通过将搜索算法限制为仅查找给定起始位置的单个匹配项来限制搜索算法,导致它为给定长度生成单个匹配项。 :ex允许从字符串的最开头开始寻找新的唯一匹配项,因此它可能会找到多个长度为 3 的匹配项; :ov只会找到一个长度为 3 的匹配项。

文档:
https://docs.perl6.org/language/regexes

Exhaustive :

To find all possible matches of a regex – including overlapping ones – and several ones that start at the same position, use the :exhaustive (short :ex) adverb



Overlapping :

To get several matches, including overlapping matches, but only one (the longest) from each starting position, specify the :overlap (short :ov) adverb:

关于raku - :ex and :ov adverbs with Perl 6 named captures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46914710/

25 4 0