use warnings; use strict; my $string = "more than one part\nanother sentence more than"; sub getone { qr/more/ } sub gettwo { qr/than/ } while ( $string =~ m{( (??{getone}) \s+ (??{gettwo}) )}xg ) { print "<$1>\n"; }