c:\@Work\Perl\monks\OldChamp>perl -wMstrict -le "use 5.010; ;; my %replacement = ( 'foo' => 'Black to move', 'bar' => 'White to move', 'zot' => 'Green to quit', ); ;; my ($string2) = map qr{ \b (?: $_) \b }xms, join ' | ', map quotemeta, reverse sort keys %replacement ; print 'regex: ', $string2; ;; my $s = 'Event ? followed by some text and then foo'; print qq{before: '$s'}; ;; $s =~ s{ Event \s+ \K [?] (?= .*? ($string2)) }{$replacement{$1}}xms; print qq{after: '$s'}; " regex: (?msx-i: \b (?: zot | foo | bar) \b ) before: 'Event ? followed by some text and then foo' after: 'Event Black to move followed by some text and then foo'