[英]Combining a Variable and Regular Expression in Perl
I have the code: 我有代码:
$TEMP =~ s/\#NAME\#/$customerName/g;
Where I am replacing #NAME#
with the value in $customername
using Regex. 我使用Regex在$customername
的值替换#NAME#
的地方。 What I want to do is append a variable onto the end of NAME
. 我要做的是将一个变量附加到NAME
的末尾。
So I want to do something like: 所以我想做类似的事情:
$TEMP =~ s/\#NAME . $appendValue\#/$customerName/g;
so it will essentially be: 因此基本上是:
$TEMP =~ s/\#NAME_1\#/$customerName/g;
Would this work or is there a proper way to handle this? 这项工作还是有适当的方法来处理呢?
Test Cases: 测试用例:
The pattern interpolates variables, so no concatenation operator is needed: 该模式对变量进行插值,因此不需要串联运算符:
$TEMP =~ s/#NAME$appendValue#/$customerName/g;
You might need to protect special characters in the variable, though, so use \\Q...\\E
: 但是,您可能需要保护变量中的特殊字符,因此请使用\\Q...\\E
:
$TEMP =~ s/#NAME\Q$appendValue\E#/$customerName/g;
#
is not special in a regex, so no backslash is needed (but it doesn't hurt). #
在正则表达式中不是特殊的,因此不需要反斜杠(但不会造成伤害)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.