首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Perl 6中触发器操作符的使用

Perl 6中触发器操作符的使用
EN

Stack Overflow用户
提问于 2018-03-14 14:39:06
回答 7查看 318关注 0票数 7

我在doc.perl6.org中看到了翻滚的使用,请参见下面的代码:

代码语言:javascript
运行
复制
my $excerpt = q:to/END/;
Here's some unimportant text.
=begin code
This code block is what we're after.
We'll use 'ff' to get it.
=end code
More unimportant text.
=begin code
I want this line.
and this line as well.
HaHa
=end code
More unimport text.
=begin code
Let's to go home.
=end code
END

my @codelines = gather for $excerpt.lines {
    take $_ if "=begin code" ff "=end code"
}

# this will print four lines, starting with "=begin code" and ending with
# "=end code"

.say for @codelines;
=begin code
This code block is what we're after.
We'll use 'ff' to get it.
=end code
=begin code
I want this line.
and this line as well.
HaHa
=end code
=begin code
Let's to go home.
=end code

我希望将=begin code=end code之间的行保存到单独的数组中,如下所示:

代码语言:javascript
运行
复制
['This code block is what we're after.', 'We'll use 'ff' to get it.']
['I want this line.', 'and this line as well.', 'HaHa']
['Let's to go home.']

我知道语法可以做到这一点,但我想知道是否有更好的方法?

EN

Stack Overflow用户

发布于 2020-05-07 03:23:48

使用梳子操作符:

代码语言:javascript
运行
复制
my $str = q:to/EOS/;
Here's some unimportant text.
=begin code
This code block is what we're after.
We'll use 'ff' to get it.
=end code
More unimportant text.
=begin code
I want this line.
and this line as well.
HaHa.
=end code
More unimport text.
=begin code
Let's go home.
=end code
EOS

my token separator { '=begin code' \n | '=end code' \n }
my token lines { [<!separator> .]+ }

say $str.comb(
/
    <lines>       # match lines that not start with
                  # =begin code or =end code
    <separator>   # match lines that start with
                  # =begin code or =end code
    <(            # start capture
        <lines>+  # match lines between
                  # =begin code and =end code
    )>            # end capture
    <separator>   # match lines that start with
                  # =begin code or =end code
/).raku;

输出:

代码语言:javascript
运行
复制
("This code block is what we're after.\nWe'll use 'ff' to get it.\n", "I want this line.\nand this line as well.\nHaHa.\n", "Let's go home.\n").Seq
票数 0
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49280568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档