Problem with findRE

I am trying to display only the first part of the summary. According to the docs this should work. Am I missing something?

{{ findRE “/.+?(?=#)/gm” .Params.Summary }}

Hugo 0.68.3
Error: add site dependencies: load resources: loading templates: “/srv/layouts/_default/summary.html:46:1”: parse failed: template: _default/summary.html:46: invalid syntax`

Hugo based on Golang, it using RE2 syntax, RE2 syntax Documentation.

You can test your regex formula here https://regex101.com/, dont forget to choose the Golang flavor.

1 Like

Ah I had checked that site but did indeed overlook the Go flavour!

Instead of a text string {{ findRE “.+?(#)/gm” .Summary }} now gives me [ ] which seems to indicate an empty array instead of the body text up to the first header.

Using flags in re2 explained in Grouping table in RE2 Documentation

but i guess this is what you want

{{ findRE `[\S\s]+#` .Summary }}

you can wrap the formula string using backtick ( ` ), so you dont need to escape backslash.

Thanks for your quick reply Pamubay.

{{ findRE [\S\s]+# .Summary }} gives the same result i.e. [ ]

btw the link https://github.com/google/re2/wiki/Syntax does not work
and https://github.com/google/re2 takes too long to load (weird)

Looks like Github down.
well i dont know the output of your .Summary, but here is the working example regex.

{{ $body := `

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

# Header 1


` }}

{{ findRE `[\S\s]+#` $body }}

Output:

[ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. #]

I might be misunderstanding what you are trying to accomplish, but there’s an excellent (and intelligent) truncate function: