How to replace backslash inside a variable?
$string = 'a\cc\ee'; $re = 'a\\cc'; $rep = "Work"; #doesnt work in variable $string =~ s/$re/$rep/og; print $string."\n"; #work with String $string =~ s/a\\cc/$rep/og; print $string."\n";
output:
a\cc\ee Work\ee
Since you use this inside a regex, you probably want quotemeta()either \Qand and \E(see perldoc perlre)
quotemeta()
\Q
\E
perldoc perlre
perl -E'say quotemeta( q[a/asf$#@ , d] )' # prints: a\/asf\$\#\@\ \,\ d # Or, with `\Q`, and `\E` $string =~ s/\Q$re\E/$rep/og; print $string."\n";
If you install $re = 'a\cc';, it will work. The backslash does not get interpolated, as you would expect when you include it in a regular expression as a variable: it is used literally in substitution.
$re = 'a\cc';
, . , - , - , , .
, $re. , , , .
$re
Perl , . .
:
$re0 = 'a\\cc'; $re1 = "a\\cc";
, :
print $re0."\n".$re1."\n"; a\\cc a\cc
, , , , , - , .
Source: https://habr.com/ru/post/1748410/More articles:How to simulate pressing a multimedia key (in C)? - cРабота с ответом AJAX с помощью методов DOM - javascriptVatir question about choosing a hidden drop-down list - ruby | fooobar.comHow to find size limits for POST on IIS using ASP.NET? - asp.netmoving CGPoint a certain distance along a certain heading ... iphone - objective-cLinq 2 SQL Grouping Question - group-byLooking for a free CMS that allows you to enter content - content-management-systemЗапустить приложение Windows x64 на С#, когда для проекта установлено значение x86 - c#Поиск Lucene занимает TOOO long - performancePassing an object to an interface when created through reflection - javaAll Articles