package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?ms)"(?:[^"]|"")*"|[^,\n]+|(?=,)(?<=,)|^|(?<=,)$`)
var str = `record 1:field 1,record 1:field 2,record 1:field 3
record 2:field 1,record 2:field 2,record 2:field 3
"quoted field","quoted field, with comma", "whitespace before & affer quote" ,"new
line in field","double quotes ""in"" field","double quotes in field "","" with comma"
,,,<- empty fields at beging, , ,<-whitespace fields,empty fields at end ->,,
^empty records`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/