Skip to content

Instantly share code, notes, and snippets.

@gongo
Created July 23, 2014 23:00
Show Gist options
  • Save gongo/97cfa21c8f629be8aec6 to your computer and use it in GitHub Desktop.
Save gongo/97cfa21c8f629be8aec6 to your computer and use it in GitHub Desktop.
Parser of text/parameters
package main
import (
"bufio"
"fmt"
"regexp"
"strings"
)
// http://tools.ietf.org/html/draft-ietf-mmusic-rfc2326bis-40#page-297
func main() {
foo := `duration: 83.124794
barparam
position: 14.467000
position
barparam :barstuff
`
scanner := bufio.NewScanner(strings.NewReader(foo))
re := regexp.MustCompile("^(?:([!-9;-~]+)[ \t]*:)? *(.+)")
for scanner.Scan() {
fmt.Printf("%q\n", re.FindAllStringSubmatch(scanner.Text(), -1))
}
}
// Result:
// [["duration: 83.124794" "duration" "83.124794"]]
// [["barparam" "" "barparam"]]
// [["position: 14.467000" "position" "14.467000"]]
// [["position" "" "position"]]
// [["barparam :barstuff" "barparam" "barstuff"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment