使用 flip-flop 和 gather-take 提取文本块儿
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
Today rains heavy.
Long live AI .
HaHa
=end code
More unimport text.
=begin code
Like to go home.
=end code
END
my @lines = gather {
my @current;
for $excerpt.lines {
if "=begin code" ^ff^ "=end code" {
# collect the values between matches
push @current, .trim;
} else {
# take the next value between matches
# don't bother if there wasn't any values matched
if @current {
# you must do something so that you aren't
# returning the same instance of the array
take @current.List;
@current = ();
}
}
}
}
.Str.say for @lines;
Output:
This code block is what we're after. We'll use 'ff' to get it.
Today rains heavy. Long live AI . HaHa
Like to go home.