my $t = 'I have 2 foo and 1 bar.'; if ( my ($first, $second) = $t =~ m{(\d+)\s+foo.*(\d+)\s+bar} ) { if ( $first == 1 + $second ) { print "matched 2:1\n"; } }