# Puppet - regular expression question

Hi, I have a question regarding regular expression in Puppet: I have some strings that contain these 2 characters: md for example: "md-abc23" , "ab_md3_4", "yymd_5f".

I want to find all the strings that contains "md" using regular expression.

I know how to find the strings that start with "md" - $mystring[0,2] but now, I want to find all the strings that contains "md" . How can I do it? Thanks a lot! edit retag close merge delete ## 1 Answer Sort by » oldest newest most voted Where are your strings? If it's just one variable, you can pattern match: if$mystring =~ /md/ {
exec { 'foo':
...
}
}

You can find out more about the =~ operator.

Assuming your strings are stored within an array ['md-abc23', 'ab_md3_4', 'yymd_5f'] you can iterate over it:

$mystrings.each |String$value| {
if $value =~ /md/ { notify { "There is${value}.": }
}
}

Alternatively you may construct an intermediate value you can then work with:

$multiple_disks =$mystrings.filter |String $value| {$value =~ /md/
}
more

thanks a lot! just a question, in regular expression we always need to put 2 slashes befire and after the pattern? (/md/)? I tried to search it in the documentation , but no luck... thanks!

( 2018-11-01 05:11:24 -0600 )edit

The initial and terminating slashes mark regular expression literals. It is like the delimiting single (or double) quotes for strings. However the =~ operator accepts stringified regular expressions, too. In the above examples you could've written string literals instead, and it works just as well.

( 2018-11-01 06:40:27 -0600 )edit