helm - replace special chars with underscore

4/26/2020

New to k8s & helm.

Trying to declare a field in a deployment using the {{ .Release.Name }}, that must not contain characters other than letters (upper + lower), digits and _.

Excluded characters should be replaced with _, for instance: feature/my-feature-1130

should replaced with: feature_my_feature_1130

Can one please help me creating such of a field?

Many Thanks in advance!

-- NI6
kubernetes-helm
regex
sprig-template-functions

2 Answers

4/26/2020

You may use regexReplaceAll like this:

{{ regexReplaceAll "\\W+" .Release.Name "_" }}

See the regex demo.

\W+ matches 1 or more occurrences of any non-word char (a char other than letter, digit and _) and replaces them with _.

The \ escaping symbol needs another escaping to form the regex escape since it is used to form string escape sequences.

Note the order of the arguments to the function, the pattern comes first, then the input string and then the replacement pattern.

-- Wiktor Stribiżew
Source: StackOverflow

4/26/2020

Are you looking for this

 String s = "feature/my-feature-1130";
          s = s.replaceAll("\\W","_");
          System.out.println(s);

output feature_my_feature_1130 sorry i don't what programming language you are using , this regex works in java , but you can try if it works for you

-- Abhinav Chauhan
Source: StackOverflow