Range with regex filter

Hello,

I’m using Githubs API for releases. It returns a JSON list, with this structure

 [
      {
           "url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
           "html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
           "assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
           "upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
           "tarball_url": "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0",
           "zipball_url": "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0",
           "id": 1,
           "tag_name": "v1.0.0",
           "target_commitish": "master",
           "name": "v1.0.0",
           ...

I would like to filter this list by a regex on the name field. I would like to match the first occurence of the regex with:

{{- range first 1 ( where $releases ".name" "=" "v1.0.[:digit:]" ) -}}

How can I do this in a correct way? I would like to filter the list and show the first matching element.
Thanks

A short additional note, I need this:

{{- $filter := (.Get "filter") -}}
{{- $releases := getJSON "https://api.github.com/repos/" (.Get "user") "/" (.Get "repo") "/releases" -}}

{{- $notfound := true -}}
{{- range $releases -}}
    {{- if and $notfound (in .name $filter) -}}
        <pre>{{- .name -}}</pre>
        {{- $notfound := false -}}
   {{- end -}}     
{{- end -}}

but $filter could be a regular expression, in this case the boolean variable does not work

Your only option today is to use findRE and check for matches.

Not tested for your problem, but I thought I would mention: in case you need some kind of mid-term processing and you are not adverse to doing it in a shell script, you can use the jq command. It’s complicated but powerful, and you can filter and manipulate json using it. I use it when grabbing json from one API, getting the json filtered into something simpler, or converted into csv, as needed before sending to another API.

@moorereason’s suggestion to use findRE may of course work, which would be the most convenient.

Perhaps we should add a match func.

2 Likes