Created
March 7, 2014 12:22
-
-
Save davidbirdsong/9410493 to your computer and use it in GitHub Desktop.
go regexp for a whirl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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