我在doc.perl6.org中看到了翻滚的使用,请参见下面的代码:
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之间的行保存到单独的数组中,如下所示:
['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.']我知道语法可以做到这一点,但我想知道是否有更好的方法?
发布于 2020-05-07 03:23:48
使用梳子操作符:
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;输出:
("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").Seqhttps://stackoverflow.com/questions/49280568
复制相似问题