Reputation: 14928
Ok so I doing the following regex replace:
Input: ([Ll])ocation
Output: \1abel
That replaces Location
with Label
and location
with label
. However, if I want to replace location
with geocode
and Location
with Geocode
, can I do that with a single regex?
I'm doing a search and replace in notepad++ at the moment, but I've had this problem in multiple regex dialects.
Upvotes: 3
Views: 1915
Reputation: 75222
EditPad Pro will do that if you select the "Adapt Case" option while replacing. It's not really a regex feature, just a feature that's supported by some tools that do search and replace. Said tools are usually powered by regexes, but that's not required; in fact, I don't think regexes would be much help in implementing such a feature.
Upvotes: 1
Reputation: 42872
In perl:
echo Location | perl -pe 's/(l)ocation/$1^lc($1)^"geocode"/ei'
Geocode
Upvotes: 0
Reputation: 8558
Here you are:
#!/usr/bin/perl -w
$str1 = 'Location Loc10';
$str2 = 'location Loc10';
$str1 =~ s/(L(?{ $g = 'G'; }) | l(?{ $g = 'g'; }) )ocation/${g}eocode/x;
print "$str1\n"; # prints Geocode Loc10
$str2 =~ s/(L(?{ $g = 'G'; }) | l(?{ $g = 'g'; }) )ocation/${g}eocode/x;
print "$str2\n"; # prints geocode Loc10
N.B. This piece of code uses experimental 'evaluate any Perl code' zero-width assertion. It is here only because you asked about it, I would not recommend using such expressions, it is much better to do several replaces. And I really doubt that Notepad++ supports it.
Upvotes: 1