我想知道是否有任何方法可以检查字符串是否存在于另一个字符串中(即包含函数)。我已经看了http://forge.puppetlabs.com/puppetlabs/stdlib,但我没有找到这个特定的功能。也许这可以通过正则表达式来实现,但我不确定该怎么做。有人可以帮我这个吗?
答案 0 :(得分:15)
# 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
答案 1 :(得分:12)
这很容易做到,请查看以下文档: http://docs.puppetlabs.com/puppet/2.7/reference/lang_conditional.html
一个简单的例子:
if $hostname =~ /^www(\d+)\./ {
notice("Welcome to web server number $1")
}