Created
September 5, 2014 08:23
-
-
Save trmcnvn/0a698ae888ccb0686e9a to your computer and use it in GitHub Desktop.
irc regex example
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 ( | |
"fmt" | |
"regexp" | |
) | |
// Valid Go regexp: https://code.google.com/p/re2/wiki/Syntax | |
var fixtures []string = []string{ | |
"PING :irc.freenode.net", // Server PING | |
":[email protected] PRIVMSG #PugBotTest :Hello, World!", // Message to channel | |
":[email protected] 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