【发布时间】:2013-10-17 11:17:57
【问题描述】:
我想知道是否有任何方法可以检查一个字符串是否存在于另一个字符串中(即包含函数)。我已经看过http://forge.puppetlabs.com/puppetlabs/stdlib,但我还没有找到这个特定的功能。也许这可以通过正则表达式实现,但我不确定该怎么做。有人可以帮我这个吗?
【问题讨论】:
我想知道是否有任何方法可以检查一个字符串是否存在于另一个字符串中(即包含函数)。我已经看过http://forge.puppetlabs.com/puppetlabs/stdlib,但我还没有找到这个特定的功能。也许这可以通过正则表达式实现,但我不确定该怎么做。有人可以帮我这个吗?
【问题讨论】:
# 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
【讨论】:
$domain in $subdomain - HOLY COW 这更容易。希望这些关键字有助于这个答案被搜索选中。
这很容易做到,请在此处查看文档: http://docs.puppetlabs.com/puppet/2.7/reference/lang_conditional.html
一个简单的例子:
if $hostname =~ /^www(\d+)\./ {
notice("Welcome to web server number $1")
}
【讨论】: