Invalid regex syntax?

Hi

In a template I’m trying to display images which are named this way :

<city>_<number between 01 and 99>_<R or V>.<extension

Here is my code :

{{ $files := .Resources.ByType "image" }}
{{ range $files }}
        {{ $toto := findRE `(.+)_(\d\d)_(.)\.(.+)` .Name }}
        {{ .Name}}@{{ index $toto 0 }}@{{ index $toto 1 }}@{{ index $toto 2 }}@{{ index $toto 3 }}@{{ index $toto 4 }}@<br>
{{ end }}

With the file Anvers_01_R.jpg it displays :

Anvers_01_R.jpg@Anvers_01_R.jpg@@@@@

Instead I was waiting for

Anvers_01_R.jpg@Anvers_01_R.jpg@Anvers@01@R@jpg@

What is wrong ?

Thanks in advance

Welsh

@Welsh or findRE returns a slice of all matches so index 0 is your match and 1, 2… are empty just one match.

to get capturing groups in Hugo use findRESubmatch this will return a list of lists

other optionswould be_

  • to use path.Basename and path.Ext use split _ on the name and delimit to merge with “@”
  • replaceRE - which support captures
    {{ range slice "Anvers_01_R.jpg" "Anvers_001_R.jpg"}}
      {{ $new := replaceRE `(.+)_(\d\d)_(.)\.(.+)` "$1@$2@$3@$4" . }}
      {{ if ne $new . }}
        {{ warnf "FILENAME VALID: %s@%s@%s" . . $new }}
      {{ else }}
        {{ warnf "FILENAME INVALID: %s@%s@%s" . . $new }}
      {{ end }}
    {{end }}
    

Hi @irkode

Thanks for the quick reply.

I have to clear my glasses :face_with_monocle: and read carefully next time.

Sorry for the misunderstood.