>perl -wMstrict -le
"my @keys = qw(F29-2 F29-3 F29-4 F44-2 F53-2 F38-3 F12-2);
my @vals = qw(F29B2 F29B3 F29B4 F44B2 F53B2 F38B3 F12B2);
my %replace;
@replace{@keys} = @vals;
;;
my $rx_search = join q{ | }, map quotemeta, keys %replace;
$rx_search = qr{ $rx_search }xms;
print $rx_search;
;;
my $s = 'F99-9 FF29-22 -F29-2- F29-2 F44-2 F12-2';
print qq{'$s'};
my $t = $s;
$t =~ s{ ($rx_search) }{$replace{$1}}xmsg;
print qq{'$t'};
;;
$t = $s;
$t =~ s{ \b ($rx_search) \b }{$replace{$1}}xmsg;
print qq{'$t'};
;;
$t = $s;
$t =~ s{ (?<! \S) ($rx_search) (?! \S) }{$replace{$1}}xmsg;
print qq{'$t'};
"
(?^msx: F29\-4 | F53\-2 | F44\-2 | F29\-2 | F38\-3 | F29\-3 | F12\-2 )
'F99-9 FF29-22 -F29-2- F29-2 F44-2 F12-2'
'F99-9 FF29B22 -F29B2- F29B2 F44B2 F12B2'
'F99-9 FF29-22 -F29B2- F29B2 F44B2 F12B2'
'F99-9 FF29-22 -F29-2- F29B2 F44B2 F12B2'
Note that none of the conversion examples use the /e switch, which will make conversion slightly faster. In all the conversion examples, F99-9 is never converted: it just doesn't appear in the conversion @keys array.
In the first conversion example, the F29-2 substring in FF29-22 and -F29-2- is converted even though it is embedded in another string: it appears in the conversion list.
This is fixed for FF29-22 in the second example by using \b boundary assertions to allow conversion only if a search string is neither preceded nor followed by a 'word' character ([A-Za-z0-9_]), but this still allows the substring in -F29-2- to be replaced because '-' is not a word character.
This problem (if problem it is) is fixed in the third example by using different boundary assertions: (?<! \S) and (?! \S) allow a match (and replacement) only if the potential match substring is neither preceded nor followed by a non-whitespace character.
$name =~ s/(F29-2,F29-3,F29-4,F44-2,F53-2,F38-3,F12-2)/$replace{$1}/;
Note that | (pipe) and not , (comma) is the alternation metacharacter.
Update: aaron_baugher, in a reply already posted, gave an example of the automatic regex generation technique discussed above, but the examples of using boundary conditions to refine a match may still be useful.
|