#!/usr/bin/perl use strict; use warnings; # https://perlmonks.org/?node_id=11148806 my $text = 'This is arbitrary just an arbitrary just example of text just arbitrary.'; @_ = qw( ju.. arb.{1,4}ary ); $text =~ m/ (?:(?:This)|(?:That)) .*? (??{ $_[ 0 ] }) .*? (??{ $_[ 1 ] }) /x and $text =~ s/ (??{ "(*FAIL)" x ! @_ }) .*? \K ( (??{ $_[ 0 ] }) ) (?{ shift @_ }) /<<<$1>>>/gx; print $text, "\n"; #### This is arbitrary <<>> an <<>> just example of text just arbitrary.