I wish to transform "eAlpha eBeta eGamma" into "fAlpha fBeta fGamma." Of course this is just a simplified example of more complex substitutions.
Here is my perl program:
my $data= "eAlpha eBeta eGamma";
$data=~ s/(e)(Alpha)|(e)(Beta)|(e)(Gamma)/f$2/g;
say $data;
The output is
fAlpha f f
Perl regex seems to remember the $1 but not the $2. Is there a way to use regex alternation, global substitution, and capture variables like $1, $2?
- There are never more than 3 alternates so I could do it in three steps but wish not to.
Any help would be appreciated.
The capture groups go from left to right without "respecting" the
|. SoBetagets captured in$4andGammain$6.But you can condense it to two groups and additionally make the first group non-capturing with
?:(or don't use a group at all, as it's just a single character, but you may have something else in there in the "real thing" that requires a group).