Get hours and minutes value from "1H30M" or "30M"


I spend a few hours today, to solve this. Im trying to solve this with use of regex

{{ findRE "(?:(\d+)H)?(?:(\d+)M)?" $.preparation_time }}

I get error invalid syntax

I then try many different one. Even this simple one I cannot get it to work.
{{ findRE "\d" $.preparation_time }} This one should return all the numbers from "1H30M" string.

Is there any difference regarding regex in hugo? I check if this works here and here I got result without a problem. Is it possible to work with capturing groups in hugo?

I added additional \ so "(?:(\d+)H) this one runs without any compile error. Just the result is not satisfying. This example returns array with just one item [1H]. I would like to achieve [1].

I will try to work now on finding the right regex, but any help from you guys will be appreciated.

I started to send you to the docs, but what you need is not documented.

{{ $duration := time.ParseDuration "1h30m" }}
{{ $duration.Hours }}

You’ll get back a Go time.Duration object.

1 Like