Skip to content

Instantly share code, notes, and snippets.

@trmcnvn
Created September 5, 2014 08:23
Show Gist options
  • Save trmcnvn/0a698ae888ccb0686e9a to your computer and use it in GitHub Desktop.
Save trmcnvn/0a698ae888ccb0686e9a to your computer and use it in GitHub Desktop.
irc regex example
package main
import (
"fmt"
"regexp"
)
// Valid Go regexp: https://code.google.com/p/re2/wiki/Syntax
var fixtures []string = []string{
"PING :irc.freenode.net", // Server PING
":hello@vevix.net PRIVMSG #PugBotTest :Hello, World!", // Message to channel
":hello@vevix.net PRIVMSG PugBot :Hello, There!", // Message to bot
}
func main() {
// Compile the regexp string and get an instance of `Regexp` back
r, err := regexp.Compile(`^(?:[:](\S+) )?(\S+)(?: ([^:].+?))?(?: [:](.+))?$`)
if err != nil {
panic(err)
}
// Loop thru each of our tests
for _, v := range fixtures {
// Doesn't match, we suck
if r.MatchString(v) == false {
fmt.Println("Well, we fucked up!")
return
}
matches := r.FindAllStringSubmatch(v, -1)
if len(matches) == 0 {
fmt.Println("Well, we fucked up!")
return
}
for _, match := range matches {
for idx, value := range match {
fmt.Printf("%d: %s\n", idx, value)
}
fmt.Println()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment