Is your intention to create a new line only upon the appearance of the word "monks?" If not, please refine your spec.
If so, split is your friend. As split explains (recommended reading!), the function offers the option to -- relevant here -- retain the term upon which you're splitting.
Here's one long-winded (and not quite perfect) approach:
#!/usr/bin/perl
use 5.014;
my @arr = ( "line1 perl monks wisdom",
"line2 monks perl wisdom",
"line3 perl perl monks monks wisdom",
"line4 perl perl monks monks monkly wisdom",
);
my $matchword = qr/monks/;
for my $line (@arr) {
if ( $line =~ $matchword ) {
my @arr_out = split /($matchword)/, $line;
for my $arr_element(@arr_out) {
say $arr_element;
}
}
}
=head OUTPUT
line1 perl
monks
wisdom
line2
monks
perl wisdom
line3 perl perl
monks
monks
wisdom
line4 perl perl
monks
monks
monkly wisdom
=cut
|