8

我想知道是否有任何方法可以检查一个字符串是否存在于另一个字符串中(即包含函数)。我已经查看了http://forge.puppetlabs.com/puppetlabs/stdlib但我还没有找到这个特定的功能。也许这可以通过正则表达式实现,但我不确定该怎么做。有人可以帮我这个吗?

4

2 回答 2

19

Puppet 中有一个“in”运算符

# 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
于 2014-05-23T11:24:50.693 回答
13

这很容易做到,在这里查看文档:http: //docs.puppetlabs.com/puppet/2.7/reference/lang_conditional.html

一个简单的例子:

if $hostname =~ /^www(\d+)\./ {
  notice("Welcome to web server number $1")
}
于 2013-10-10T00:47:59.853 回答