如何在perl6中找到与regexp匹配的数量? [英] How to find number of matches to regexp in perl6?

查看:34
本文介绍了如何在perl6中找到与regexp匹配的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Perl 5 中我们可以编写

In Perl 5 we can write

my @things = $text =~ /thing/g;

$things 在标量上下文中是字符串 $text 中子字符串 thing 的非重叠出现次数.

And $things in scalar context is number of non-overlapping occurrences of substring thing in string $text.

如何在 Perl 6 中执行此操作?

How to do this in Perl 6?

推荐答案

你可以这样做:

my $text = 'thingthingthing'
my @things = $text ~~ m:g/thing/;
say +@things; # 3

~~ 将左侧与右侧匹配,m:g 使测试返回一个 List[Match] 包含所有结果.

~~ matches the left side against the right side, m:g makes the test return a List[Match] containing all the results.

这篇关于如何在perl6中找到与regexp匹配的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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