Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active April 20, 2025 11:52

Revisions

  1. ik5 revised this gist Jun 24, 2024. No changes.
  2. ik5 created this gist Jun 24, 2024.
    45 changes: 45 additions & 0 deletions lpa_regex_parsing.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    package main

    // go play with my original code: https://go.dev/play/p/dt8WFHM1E8d

    import (
    "fmt"
    "regexp"
    )

    var validLPA = []string{
    `LPA$www1.a.google.com$ABC1`,
    `LPA$www1.a.google.com$ABC1-X$1.1.1$1`,
    `LPA$www1.a.google.com$ABC1$$1`,
    `LPA$www1.a.google.com$ABC1$10000.2.3333333`,
    }

    const activationCodeRegex = `^((?P<LPA>LPA)\$(?P<FQDN>([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})(\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})*?(\.[a-zA-Z]{1}[a-zA-Z0-9]{0,62})\.?)\$(?P<AC_TOKEN>[A-Z0-9-]+)(\$(?P<OID>([0-9]+)(\.[0-9]+)*)?(\$(?P<CONFIRMATION_CODE>1))?)?$)`

    var (
    activationCodeRegexp = regexp.MustCompile(activationCodeRegex)
    groupNames = activationCodeRegexp.SubexpNames()
    )

    func main() {
    fmt.Printf("groupNames: %+#v\n", groupNames)
    var (
    matches [][]string
    result []map[string]string
    )
    for _, lpaString := range validLPA {
    matches = activationCodeRegexp.FindAllStringSubmatch(lpaString, -1)
    fmt.Printf("%s - %#+v\n", lpaString, matches)

    var tmpMap = map[string]string{}
    for idx, name := range groupNames {
    if name != "" && idx > 0 {
    tmpMap[name] = matches[0][idx]
    }
    }
    result = append(result, tmpMap)

    }

    fmt.Printf("result: %+#v\n", result)
    }