Ask Your Question

Puppet - regular expression question

asked 2018-10-31 08:57:40 -0600

Dan1 gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-10-31 10:43:09 -0600

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/
edit flag offensive delete link 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!

Dan1 gravatar imageDan1 ( 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.

Kai Burghardt gravatar imageKai Burghardt ( 2018-11-01 06:40:27 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-10-31 08:57:40 -0600

Seen: 52 times

Last updated: Oct 31