Regex match between Hugo tags

Viewed 125

I have a Hugo tag that looks like this:

{{< ref posts/filename >}}

filename is a variable and is the bit I want to be left with.

So far I've got this:

/\{\{\< (.*?) \>\}\}\)/

Which leaves me with:

ref posts/filename

But I can't figure out how to remove the rest before the filename

2 Answers

You can match any chars other than > and / up to the first / from the {{< part:

\{\{<[^>\/]*\/(.*?) >\}\}

See the regex demo.

Details:

  • \{\{< - a {{< substring
  • [^>\/]* - zero or more chars other than > and /
  • \/ - a / char
  • (.*?) - Group 1: any zero or more chars other than line break chars, as few as possible
  • >\}\} - a >}} substring.

See the regex graph:

enter image description here

Related