Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用perl动态替换_Regex_Bash_Perl - Fatal编程技术网

Regex 用perl动态替换

Regex 用perl动态替换,regex,bash,perl,Regex,Bash,Perl,我想通过编程将小占位符替换为静态内容,如用户名或工作路径 bash中是否有可能动态执行perl替换状态,如: GROUPNAME="$(id -g -n $USER)" perl -pi -e 's/\(PLACEHOLDER\)/' + "$GROUPNAME" + '/g' filepath/file 如果我也能将这种动态行为应用于占位符,那将是非常好的。但首先要做的是。如何连接这些字符串?删除“+”(空格和加号)。Bash对相邻字符串进行自动连接 echo 'hi!'t"here" #

我想通过编程将小占位符替换为静态内容,如用户名或工作路径

bash中是否有可能动态执行perl替换状态,如:

GROUPNAME="$(id -g -n $USER)"
perl -pi -e 's/\(PLACEHOLDER\)/' + "$GROUPNAME" + '/g' filepath/file
如果我也能将这种动态行为应用于占位符,那将是非常好的。但首先要做的是。如何连接这些字符串?

删除
“+”
(空格和加号)。Bash对相邻字符串进行自动连接

echo 'hi!'t"here"  # hi!there
以这种方式生成Perl代码是安全的,因为
id-g-n$USER
的输出不会包含您想要的
\
$
@
/

GROUPNAME="$(id -g -n $USER)"
perl -i -pe's/\(PLACEHOLDER\)/'"$GROUPNAME"'/g' filepath/file
但是没有理由生成Perl代码。这很容易出错。相反,请使用以下选项之一:

export GROUPNAME="$(id -g -n $USER)"
perl -i -pe's/\(PLACEHOLDER\)/$ENV{GROUPNAME}/g' filepath/file


不确定这是否是一个很好的匹配,因为它是关于变量的。@ikegami:这个用例是安全的。为了清楚起见,可以使用
sed
。它来自可信的输入(
id
)。
GROUPNAME="$(id -g -n $USER)" perl -i -pe's/\(PLACEHOLDER\)/$ENV{GROUPNAME}/g' filepath/file