Home > Blockchain >  Extracting string between two characters in helm
Extracting string between two characters in helm

Time:02-11

I am trying to extract all strings between ( and ) from values.yaml in _helper.tpl.

In my _helper.tpl

{{- define "firstchart.extraction" -}}
{{- regexFindAll "(?<=\()(.*?)(?=\))"  .Values.monitor.url -1 -}}
{{- end -}}

my values.yaml file

monitor:
   url: "(zone-base-url)/google.com/(some-random-text)"

so i want to extract zone-base-url and some-random-text. How to do this?

CodePudding user response:

It looks like the regex library is RE2 that does not support lookarounds of any type. That means, you need to "convert" non-consuming lookarounds into consuming patterns, still keeping the capturing group in between the \( and \) delimiters:

{{- regexFindAll "\\((.*?)\\)"  .Values.monitor.url -1 -}}

Or,

{{- regexFindAll "\\(([^()]*)\\)"  .Values.monitor.url -1 -}}

Details:

  • \( - a ( char
  • (.*?) - Group 1: any zero or more chars other than line break chars as few as possible ([^()]* matches zero or more chars other than ( and ) chars)
  • \) - a ) char

The -1 argument extracts just the Group 1 contents.

A literal backslash in the pattern must be doubled to represent a literal backslash.

  • Related