About | FAQ | Help
Ask Your Question
0

Regsubst not picking up the groups

asked 2017-12-21 10:54:42 -0500

itaremyinitials gravatar image

I have a variable with a value : [192.168.12.40] I would like to get rid of the curly brackets, so I decided to use regsubst. And this is how I am using it given that var1=[192.168.12.40]

  $var2=regsubst($var1,'(\[)(.*?)(\])','\2')
  notify {"This is the value for ipaddress ${var2} ":}

Unfortunately, this is not returning the correct value that I want which is 192.168.12.40 without the curly brackets. I need help figuring out what am I missing.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-12-21 13:43:08 -0500

stivesso gravatar image

updated 2017-12-21 13:45:37 -0500

Hi itaremyinitials,

It seems like the main reason for that wrong output comes from the way $var1 was defined, if it is defined without simple quote, then it is most probably interpreted as an array (not as a String), you must have a single quote to make sure that the curly brackets are not interpreted (see the output below).

With this code, I get the correct output

$var1='[192.168.12.40]'
$var2=regsubst($var1,'(\[)(.*?)(\])','\2')
notify {"This was the value before: ${var1} ":}
notify {"This is the value after: ${var2} ":}

Output,

Notice: This was the value before: [192.168.12.40]
Notice: /Stage[main]/Main/Notify[This was the value before: [192.168.12.40] ]/message: defined 'message' as 'This was the value before: [192.168.12.40] '
Notice: This is the value after: 192.168.12.40
Notice: /Stage[main]/Main/Notify[This is the value after: 192.168.12.40 ]/message: defined 'message' as 'This is the value after: 192.168.12.40 '
Notice: Applied catalog in 0.16 seconds
edit flag offensive delete link more

Comments

Thanks a lot @stivesso that was it

itaremyinitials gravatar imageitaremyinitials ( 2017-12-27 01:48:50 -0500 )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

1 follower

Stats

Asked: 2017-12-21 10:54:42 -0500

Seen: 13 times

Last updated: Dec 21 '17