Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace [ and ] in a string with regex

Tags:

regex

puppet

How to delete [ and ] characters in a string with Regex ?

I'm using the Puppet DSL function regsubst :

regsubst($::env, '\[\]', '', 'G')

thanks

like image 434
PapelPincel Avatar asked Dec 10 '25 14:12

PapelPincel


1 Answers

Brackets are metacharacters in a regex and need to be escaped (and put in a character class if you want to match either one):

regsubst($::env, '[\[\]]', '', 'G')

Your version was only matching the exact string [].

In a regex, [abc] means "Match one of the following: a, b or c".

like image 154
Tim Pietzcker Avatar answered Dec 13 '25 20:12

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!