Is there a regex function for kubernetes helm templates available?

Yes there is. However it is unfortunately undocumented.
Helm uses the sprig library to provide additional functions for templating. What is undocumented is many of the sprig functions that have been added to the code (ref issue #84), and the regex functions are some of them. You can find these functions here: https://github.com/Masterminds/sprig/blob/master/regex.go

Thus you can do {{ .Chart.AppVersion | regexFind "\d+\.\d+" }}

(note that I also added a \ before the . to escape it as . means to match any character, which does not seem to be your intent)

 

Not your use case, but one item of note for others which might be interested in using the regex* functions from sprig, is that unfortunately the sprig owner made the subject string the second argument of all the functions, and some of the functions have additional arguments after it (e.g. the regexReplaceAll function). The reason why this is a problem is that when a function is used in a pipeline, the result of the previous element in the pipeline becomes the last argument of the function. So this does not work: "foo subject string" | regexReplaceAll "foo" "bar", as it'll treat bar as the string to operate on, and foo subject string as the string to replace any occurrences of foo with (which in this example there would not be any).


Had to deal with this today, you will want something like:

{{ regexFind "\\d+\\.\\d+" .Chart.AppVersion }}

Requires the double escapes due to yaml.

Docs for this

Tags:

Kubernetes