func main() { bytes, err := ioutil.ReadAll(os.Stdin) if err != nil { fmt.Fprintf(os.Stderr, "can't read input: %s\n", err) os.Exit(2) } ilen := len(bytes) // Delete the comment lines and newlines bytes = pcre.MustCompile("(>[^\n]+)?\n", 0). ReplaceAll(bytes, []byte{}, 0) clen := len(bytes) for _, s := range variants { fmt.Printf("%s %d\n", s, countMatches(s, bytes)) } for _, sub := range substs { bytes = pcre.MustCompile(sub.pat, 0). ReplaceAll(bytes, []byte(sub.repl), 0) } fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes)) }
func countMatches(pat string, bytes []byte) int { re := pcre.MustCompile(pat, 0) n := 0 for { e := re.FindIndex(bytes, 0) if e == nil { break } n++ bytes = bytes[e[1]:] } return n }
func main() { runtime.GOMAXPROCS(4) bytes, err := ioutil.ReadAll(os.Stdin) if err != nil { fmt.Fprintf(os.Stderr, "can't read input: %s\n", err) os.Exit(2) } ilen := len(bytes) // Delete the comment lines and newlines bytes = pcre.MustCompile("(>[^\n]+)?\n", 0).ReplaceAll(bytes, []byte{}, 0) clen := len(bytes) mresults := make([]chan int, len(variants)) var i int var s string for i, s = range variants { ch := make(chan int) mresults[i] = ch go func(intch chan int, ss string) { intch <- countMatches(ss, bytes) }(ch, s) } lenresult := make(chan int) bb := bytes go func() { for _, sub := range substs { bb = pcre.MustCompile(sub.pat, 0).ReplaceAll(bb, []byte(sub.repl), 0) } lenresult <- len(bb) }() for i, s = range variants { fmt.Printf("%s %d\n", s, <-mresults[i]) } fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, <-lenresult) }
func main() { m := pcre.MustCompile("abc", pcre.CASELESS).MatcherString("Abc", 0) fmt.Printf("pcre 1: %v\n", m.Matches()) m = pcre.MustCompile("abc", 0).MatcherString("Abc", 0) fmt.Printf("pcre 2: %v\n", m.Matches()) m = pcre.MustCompile("<([A-Z][A-Z0-9]*)\\b[^>]*>.*?</\\1>", pcre.CASELESS).MatcherString("<H1>foobar</H1>", 0) fmt.Printf("pcre 3: %v\n", m.Matches()) m = pcre.MustCompile("<([A-Z][A-Z0-9]*)\\b[^>]*>.*?</\\1>", pcre.CASELESS).MatcherString("<H1>foobar</H2>", 0) fmt.Printf("pcre 4: %v\n", m.Matches()) m = pcre.MustCompile("abc", 0).MatcherString("Abc", 0) fmt.Printf("pcre 5: %v\n", m.Matches()) re := pcre.MustCompile("foo", 0) result := re.ReplaceAll([]byte("I like foods."), []byte("car"), 0) fmt.Printf("pcre 6: %v\n", string(result)) result = re.ReplaceAll([]byte("food fight fools foo"), []byte("car"), 0) fmt.Printf("pcre 7: %v\n", string(result)) }
import ( "crypto/md5" "fmt" "github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre" "io" "regexp" "strings" ) var spaceRe *regexp.Regexp = regexp.MustCompile(`\s+`) var nullRe *regexp.Regexp = regexp.MustCompile(`\bnull\b`) var limitRe *regexp.Regexp = regexp.MustCompile(`\blimit \?(?:, ?\?| offset \?)?`) var escapedQuoteRe *regexp.Regexp = regexp.MustCompile(`\\["']`) //var doubleQuotedValRe *regexp.Regexp = regexp.MustCompile(`".*?"`) var doubleQuotedValRe pcre.Regexp = pcre.MustCompile(`".*?"`, 0) var singleQuotedValRe *regexp.Regexp = regexp.MustCompile(`'.*?'`) var number1Re *regexp.Regexp = regexp.MustCompile(`\b[0-9+-][0-9a-f.xb+-]*|[xb.+-]\?`) var number2Re *regexp.Regexp = regexp.MustCompile(`[xb.+-]\?`) var valueListRe *regexp.Regexp = regexp.MustCompile(`\b(in|values?)(?:[\s,]*\([\s?,]*\))+`) var multiLineCommentRe *regexp.Regexp = regexp.MustCompile(`(?sm)/\*[^!].*?\*/`) var orderByAscRe *regexp.Regexp = regexp.MustCompile(`(?i)order by (\S+) asc\b`) // Go re doesn't support ?=, but I don't think slow logs can have -- comments, // so we don't need this for now //var oneLineCommentRe *regexp.Regexp = regexp.MustCompile(`(?:--|#)[^'"\r\n]*(?=[\r\n]|\z)`) var oneLineHashCommentRe *regexp.Regexp = regexp.MustCompile(`#[^'"\r\n]*([\r\n]|\z)`) var useDbRe *regexp.Regexp = regexp.MustCompile(`\Ause .+\z`) var unionRe *regexp.Regexp = regexp.MustCompile(`\b(select\s.*?)(?:(\sunion(?:\sall)?)\s$1)+`) var adminCmdRe *regexp.Regexp = regexp.MustCompile(`\Aadministrator command: `) var storedProcRe *regexp.Regexp = regexp.MustCompile(`(?i)\A\s*(call\s+\S+)\(`)
[]string{}, []interface{}{1, 2}, // set metric 1 "bar" []interface{}{}, thread{pc: 0, matches: map[int][]string{}}, }, {"set str", instr{set, 2}, []*pcre.Regexp{}, []string{}, []interface{}{1, "2"}, []interface{}{}, thread{pc: 0, matches: map[int][]string{}}, }, {"match", instr{match, 0}, []*pcre.Regexp{pcre.MustCompile("a*b")}, []string{}, []interface{}{}, []interface{}{}, thread{match: true, pc: 0, matches: map[int][]string{0: {"aaaab"}}}, }, {"cmp lt", instr{cmp, -1}, []*pcre.Regexp{}, []string{}, []interface{}{1, "2"}, []interface{}{}, thread{pc: 0, match: true, matches: map[int][]string{}}}, {"cmp eq", instr{cmp, 0}, []*pcre.Regexp{},