如何在 Perl 6 的正则表达式中放置一个子? [英] How to put a sub inside a regex in Perl 6?

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

问题描述

这就是我想要做的.

>  my sub nplus1($n) {$n +1}
> my regex nnplus1 { ^ (\d+) &nplus1($0) $ }
> "123" ~~ &nnplus1
P6opaque: no such attribute '$!pos' in type Match...

推荐答案

<{...}> 结构在正则表达式中运行 Perl 6 代码,并将结果作为正则表达式求值:

The <{...}> construct runs Perl 6 code inside a regex, and evaluates the result as a regex:

<代码>我的子 nplus1($n) {$n +1}我的正则表达式 nnplus1 { ^ (\d+) <{ nplus1($0) }>$ }这么说'23'~~ &nnplus1;# 输出:真这么说'22'~~ &nnplus1;# 输出:假

这篇关于如何在 Perl 6 的正则表达式中放置一个子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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