开发者

Multiple substitutions with a single regular expression in perl

开发者 https://www.devze.com 2023-01-04 22:24 出处:网络
Say I have the following in perl: my $string; $string =~ s/ /\\\\ /g; $string =~ s/\'/\\开发者_Python百科\\\'/g;

Say I have the following in perl:

my $string;
$string =~ s/ /\\ /g;
$string =~ s/'/\开发者_Python百科\'/g;
$string =~ s/`/\\`/g;

Can the above substitutions be performed with a single combined regular expression instead of 3 separate ones?


$string =~ s/([ '`])/\\$1/g;

Uses a character class [ '`] to match one of space, ' or ` and uses brackets () to remember the matched character. $1 is then used to include the remembered character in the replacement.


Separate substitutions may be much more efficient than a single complex one (e.g. when working with fixed substrings). In such cases you can make the code shorter, like this:

my $string;    
for ($string) {
    s/ /\\ /g;
    s/'/\\'/g;
    s/`/\\`/g;
}


Although it's arguably easier to read the way you have it now, you can perform these substitutions at once by using a loop, or combining them in one expression:

# loop
$string =~ s/$_/\\$_/g foreach (' ', "'", '`');

# combined
$string =~ s/([ '`])/\\$1/g;

By the way, you can make your substitutions a little easier to read by avoiding "leaning toothpick syndrome", as the various regex operators allow you to use a variety of delimiters:

$string =~ s{ }{\\ }g;
$string =~ s{'}{\\'}g;
$string =~ s{`}{\\`}g;
0

精彩评论

暂无评论...
验证码 换一张
取 消