Use regex in helm template

Viewed 2725

I'm trying to provide a condition inside my helm template to check for a valid (or a rather invalid) hostname using regexMatch function.

Here's my line of code that I'm using:

{{- if regexMatch "(?:[0-9]{1,3}\.){3}[0-9]{1,3}$" .Values.hostname }}

The related part in the values file is a simple one:

hostname: 10.10.10.10

However, I'm hitting a syntax error with no real explanation:
Error: parse error at (api-gateway/templates/ingress.yml:1): invalid syntax

When I've tried to use the example from the docs: https://helm.sh/docs/chart_template_guide/function_list/#regexmatch-mustregexmatch it obviously worked, so I wonder why my code isn't working.

1 Answers

Turns out that I needed to use a double backslash to make it work:

{{- if regexMatch "(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$" .Values.hostname }}
Related