Function in puppet for checking if a string contains another string

hveiga picture hveiga · Oct 9, 2013 · Viewed 24.8k times · Source

I would like to know if there is any way of checking if a string exists inside another string (ie contains function). I have been taken a look to http://forge.puppetlabs.com/puppetlabs/stdlib but I haven't found this specific function. Maybe this is possible through a regexp, but I am not really sure how to do it. Can anybody help me this one?

Answer

Sharas picture Sharas · May 23, 2014

There is an "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