Here's a guess at something that might be a little like what you think you may need (requires Perl version 5.10 or greater):
Please see perlre, perlretut, and perlrequick.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'
Update:
... a string2, which is spacebspace- or spacewspace-.I don't understand what "spacebspace-" and "spacewspace-" are supposed to be. Please clarify, perhaps with an example string like
Give a man a fish: <%-{-{-{-<
In reply to Re: regex replace a string
by AnomalousMonk
in thread regex replace a string
by OldChamp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |