[Golang] Regular Expression Named Group - Extract Metadata from File Path


Golang regexp example of named group matches - extract metadata from file path.

Problem

We have a string of file path:

articles/2016/01/05/deploy-website-by-pelican-travis-ci-github-pages%en.rst

We want to extract the metadata from the string in the following form:

date - 2016/01/05

slug - deploy-website-by-pelican-travis-ci-github-pages

lang - en

Solution

Run Code on Go Playground

package main

import (
        "fmt"
        "regexp"
)

var path1 = "articles/2016/01/05/deploy-website-by-pelican-travis-ci-github-pages%en.rst"
var path2 = "articles/2016/01/21/everything-is-teaching-us-ajahn-chah%zh.rst"

func main() {
        pattern := `articles/(?P<date>\d{4}/\d{2}/\d{2})/(?P<slug>[-a-zA-Z0-9]*)%(?P<lang>[_a-zA-Z]{2,5})\.rst`
        pathMetadata := regexp.MustCompile(pattern)

        matches := pathMetadata.FindStringSubmatch(path1)
        names := pathMetadata.SubexpNames()
        for i, match := range matches {
                if i != 0 {
                        fmt.Println(names[i], match)
                }
        }

        matches = pathMetadata.FindStringSubmatch(path2)
        names = pathMetadata.SubexpNames()
        for i, match := range matches {
                if i != 0 {
                        fmt.Println(names[i], match)
                }
        }
}

Output

Output of above code:

date 2016/01/05
slug deploy-website-by-pelican-travis-ci-github-pages
lang en
date 2016/01/21
slug everything-is-teaching-us-ajahn-chah
lang zh

Appendix

Another example:

Run Code on Go Playground

package main

import (
        "fmt"
        "regexp"
)

var path = "articles/anya/visuddhimagga/visuddhimagga-chap01%zh.rst"

func main() {
        pattern := `articles/(?P<urlpath>[-a-zA-Z0-9/]*)/(?P<slug>[-a-zA-Z0-9]*)%(?P<lang>[_a-zA-Z]{2,5})\.rst`
        pathMetadata := regexp.MustCompile(pattern)

        matches := pathMetadata.FindStringSubmatch(path)
        names := pathMetadata.SubexpNames()
        for i, match := range matches {
                if i != 0 {
                        fmt.Println(names[i], match)
                }
        }
}

output:

urlpath anya/visuddhimagga
slug visuddhimagga-chap01
lang zh

Yet another example:

Run Code on Go Playground

package main

import (
        "fmt"
        "regexp"
)

var path = "articles/anya/visuddhimagga/visuddhimagga-chap01%zh.rst"

func main() {
        pattern := `articles[-a-zA-Z0-9/]*/(?P<slug>[-a-zA-Z0-9]*)%(?P<lang>[_a-zA-Z]{2,5})\.rst`
        pathMetadata := regexp.MustCompile(pattern)

        matches := pathMetadata.FindStringSubmatch(path)
        names := pathMetadata.SubexpNames()
        for i, match := range matches {
                if i != 0 {
                        fmt.Println(names[i], match)
                }
        }
}

output:

slug visuddhimagga-chap01
lang zh

References:

[1]python regular expression ?P - Google Search
[2]golang named regular expression - Google Search
[3]golang named path metadata - Go Playground
[4]Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript
[5]