Regex group mismatch

Hi,

I am unable to get Regex Groups working and need some help. Below code explains my issue.

{{ $value := "Field: This field actually has some useful usage and this is just some sample text.\n" }}

replaceRE:

1. {{- (replaceRE `^.+:\s+(.*)$` `$1` $value 1) }}

2. {{- (replaceRE `:\s+(.*)$` `$1` $value 1) }}

{{/* 
// actal outpus: Field: This field actually has some useful usage and this is just some sample text.\n

// expected ouput: This field actually has some useful usage and this is just some sample text.

// on regex101 (go flavour), regex both code aptly shows the group
*/}}

findRE: {{ (findRE `:\s+(.*)$` $value 1) }}

{{/* 
// actual ouput: []

// expected ouput: ["This field actually has some useful usage and this is just some sample text.\n"]

// the problem seems more evident when \n is present in the $value
*/}}

Thanks.

Your “value” ends with a new line, and by default the dot doesn’t match new lines. Do this instead.

replaceRE `(?s)^.+:\s+(.*)$` `$1` $value 1 
1 Like

Thanks. It works!

But when I use the pattern from point 2 above, I am not getting the expected match.

Maybe I’m making some fundamental Regex mistake?
Also, is there any way to use groups in findRE?

{{ $value := "Field: This field actually has some useful usage and this is just some sample text.\n" }}

replaceRE: {{ replaceRE `(?s):\s+(.*)$` `$1` $value 1 }}
// actual output: FieldThis field actually has some useful usage and this is just some sample text.\n
// expected output: This field actually has some useful usage and this is just some sample text.
// on regex101 (golang flavour), regex code aptly shows the group

findRE: {{ findRE `(?s):\s+(.*)$` $value 1 }}
// actual output: [": This field actually has some useful usage and this is just some sample text.\n"]
// expected output: ["This field actually has some useful usage and this is just some sample text.\n"]
// is there any way to use groups in findRE?

I think you’re overcomplicating it.

replaceRE `^.*?:\s+` "" $value

should do what you want, namely remove everything from the beginning of the string up to and including the first colon. What remains is what you’re asking for. No need for matching the newline at the end, no need for a capturing group.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.