Random replace for caseless search with regex

So, I am doing the following regex:

 Input: ([Ll])ocation
Output: \1abel

This replaces Locationwith Labeland Locationby Label. However, if I want to replace Locationwith geocodeand Locationwith geocode, can I do this with one regex?

I'm doing a search and replace in notepad ++ at the moment, but I had this problem in several regex dialects.

+3
source share
3 answers

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. Perl- . , , , . , Notepad ++ .

+1

EditPad Pro , "Adapt Case" . , , , . , ; , , .

+1

In perl:

echo Location | perl -pe 's/(l)ocation/$1^lc($1)^"geocode"/ei'
Geocode
0
source

Source: https://habr.com/ru/post/1730242/


All Articles