Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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 如何在puppet上使用参数化正则表达式(例如/${user}/)进行测试?_Regex_Puppet - Fatal编程技术网

Regex 如何在puppet上使用参数化正则表达式(例如/${user}/)进行测试?

Regex 如何在puppet上使用参数化正则表达式(例如/${user}/)进行测试?,regex,puppet,Regex,Puppet,我需要获取用户的主目录。我决定通过解析::getent_passwd字符串(这是一个自定义的事实构建)来获得它 连接/etc/passwd)的内容 并在正则表达式的帮助下提取相关信息 当我使用固定字符串(“adam”)测试::getent时,提取工作: if "$::getent_passwd" =~ /\|adam:x:[^:]+:[^:]+:[^:]*:([^:]*):/ { $user_home = $1 notify{"This works":} } 但是,当我使用$user变

我需要获取用户的主目录。我决定通过解析::getent_passwd字符串(这是一个自定义的事实构建)来获得它 连接
/etc/passwd
)的内容 并在正则表达式的帮助下提取相关信息

当我使用固定字符串(“
adam
”)测试
::getent
时,提取工作:

if "$::getent_passwd" =~ /\|adam:x:[^:]+:[^:]+:[^:]*:([^:]*):/ {
  $user_home = $1
  notify{"This works":}
}
但是,当我使用
$user
变量构建正则表达式时,没有任何匹配项:

if "$::getent_passwd" =~ /\|${user}:x:[^:]+:[^:]+:[^:]*:([^:]*):/ {
  $user_home = $1
} else {
  fail{"this fails":}
}

客户端和服务器在Ubuntu 14.04 64位上使用Puppet 3.7.3。Puppetserver使用puppetdb。

在puppet正则表达式中使用普通变量似乎是不可能的:

在上面的链接中提出了解决方法:

regex.erb:


五年后有了答案,但现在有了一个简单的方法。 对于在这里登陆的其他人。。。尝试从字符串生成regexp:

if("abcd" =~ Regexp("${myvar}") { (...) }

非常感谢。这真让我大吃一惊。对于这种基本且无处不在的功能的漫游需求给语言蒙上了阴影。我听说Puppet 4在这方面不会那么麻烦。
$testre = template('regex.erb')
if $testre == "yes\n" { notify{"success!": } }
if("abcd" =~ Regexp("${myvar}") { (...) }