puppet 中用于检查字符串是不是包含另一个字符串的函数
Posted
技术标签:
【中文标题】puppet 中用于检查字符串是不是包含另一个字符串的函数【英文标题】:Function in puppet for checking if a string contains another stringpuppet 中用于检查字符串是否包含另一个字符串的函数 【发布时间】:2013-10-17 11:17:57 【问题描述】:我想知道是否有任何方法可以检查一个字符串是否存在于另一个字符串中(即包含函数)。我已经看过http://forge.puppetlabs.com/puppetlabs/stdlib,但我还没有找到这个特定的功能。也许这可以通过正则表达式实现,但我不确定该怎么做。有人可以帮我这个吗?
【问题讨论】:
【参考方案1】:有一个"in" operator in Puppet:
# Right operand is a string:
'eat' in 'eaten' # resolves to true
'Eat' in 'eaten' # resolves to true
# Right operand is an array:
'eat' in ['eat', 'ate', 'eating'] # resolves to true
'Eat' in ['eat', 'ate', 'eating'] # resolves to true
# Right operand is a hash:
'eat' in 'eat' => 'present tense', 'ate' => 'past tense' # resolves to true
'eat' in 'present' => 'eat', 'past' => 'ate' # resolves to false
# Left operand is a regular expression (with the case-insensitive option "?i")
/(?i:EAT)/ in ['eat', 'ate', 'eating'] # resolves to true
# Left operand is a data type (matching integers between 100-199)
Integer[100, 199] in [1, 2, 125] # resolves to true
Integer[100, 199] in [1, 2, 25] # resolves to false
【讨论】:
@hveiga 这应该是被接受的答案,它比当前接受的答案(正则表达式)更好地回答了这个问题。 ...但是如果使用内联示例,这个答案会更好。 嗯,这很容易记录!我花了一个小时寻找关闭正则表达式字符串插值或 puppet 函数来转义正则表达式字符的方法,以便我可以将包含点/句点/特殊字符的变量中的字符串与另一个包含点的字符串进行比较和匹配(在我的案例,子域)。$domain in $subdomain
- HOLY COW 这更容易。希望这些关键字有助于这个答案被搜索选中。【参考方案2】:
这很容易做到,请在此处查看文档: http://docs.puppetlabs.com/puppet/2.7/reference/lang_conditional.html
一个简单的例子:
if $hostname =~ /^www(\d+)\./
notice("Welcome to web server number $1")
【讨论】:
链接已损坏,您可以改用puppet.com/docs/puppet/5.3/…。以上是关于puppet 中用于检查字符串是不是包含另一个字符串的函数的主要内容,如果未能解决你的问题,请参考以下文章
在 JavaScript 中检查字符串是不是包含另一个子字符串的最快方法?
C# - 检查字符串是不是以相同的顺序包含另一个字符串的字符