Skip to content

Instantly share code, notes, and snippets.

@davidbirdsong
Created March 7, 2014 12:22
Show Gist options
  • Save davidbirdsong/9410493 to your computer and use it in GitHub Desktop.
Save davidbirdsong/9410493 to your computer and use it in GitHub Desktop.
go regexp for a whirl
package main
import (
"regexp"
"fmt"
)
var reg string = `^(?P<RemoteIP>\S+) \S+ \S+ \[(?P<Timestamp>[^\]]+)\] "(?P<Method>[A-Z]+) (?P<Url>[^\s]+)[^"]*" (?P<StatusCode>\d+) (?P<RequestSize>\d+)`
var line string = "65.32.144.5 - - [07/Mar/2014:15:36:34 +0400] \"POST /api/listAddresses HTTP/1.1\" 200 290"
func main() {
m, err := regexp.Compile(reg)
if err != nil { panic(err); }
res := m.FindStringSubmatch(line)
if res == nil {
panic(fmt.Errorf("no match "))
}
for index, name := range m.SubexpNames() {
if index == 0 {
continue
}
if name == "" {
name = fmt.Sprintf("%d", index)
}
fmt.Printf("%v %v\n", name, res[index])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment