func logFilenameToTime(filename string, filenameToTimeRegex *regexp.Regexp) (time.Time, error) { if filenameToTimeRegex == nil { return time.Now(), fmt.Errorf("filename_to_time_regex is not set in indexer configuration") } n1 := filenameToTimeRegex.SubexpNames() r2s := filenameToTimeRegex.FindAllStringSubmatch(filename, -1) if len(r2s) == 0 { return time.Now(), fmt.Errorf("filename_to_time_regex did not match %q", filename) } r2 := r2s[0] md := map[string]string{} for i, n := range r2 { md[n1[i]] = n } getOrZero := func(key string) int { val, exists := md[key] if !exists { return 0 } num, err := strconv.Atoi(val) if err != nil { return 0 } return num } year := getOrZero("year") month := time.Month(getOrZero("month")) day := getOrZero("day") hour := getOrZero("hour") minute := getOrZero("minute") return time.Date(year, month, day, hour, minute, 0, 0, time.UTC), nil }
// parseEntries parses ENTRY elements. func (a *AsxParser) parseEntries(entries [][]string) { // Regular expression to match stream URL var urlr *regexp.Regexp var s *Stream // Go over all found entries for idx, entry := range entries { s = NewStream(idx) // First we parse all the info we can get except the URL to a stream for _, reg := range asxRegs { // We do this one last if reg.name == "Url" { urlr = reg.reg continue } values := reg.reg.FindStringSubmatch(entry[1]) var value string if len(values) == 2 { value = values[1] } else { value = a.getValue(reg.name) } s.setValue(reg.name, value) } // Inherit base for URLs from main playlist body if s.Base != "" { s.Base = a.Base } // Make sure the base ends with "/" if s.Base != "" && !strings.HasSuffix(s.Base, "/") { s.Base += "/" } // Find all the stream URLs streams := urlr.FindAllStringSubmatch(entry[1], -1) for _, stream := range streams { newStream := s.makeCopy() // Prefix base URL to the stream URL if newStream.Base != "" { stream[1] = newStream.Base + stream[1] } newStream.Url = stream[1] a.Streams = append(a.Streams, newStream) } } }
//broker.handleMessage() gets messages from broker.This() and handles them according // to the user-provided plugins currently loaded. func (b *Broker) handleMessage(thingy map[string]interface{}) { if b.cbIndex[M] == nil { return } message := new(Event) jthingy, _ := json.Marshal(thingy) json.Unmarshal(jthingy, message) message.Broker = b botNamePat := fmt.Sprintf(`^(?:@?%s[:,]?)\s+(?:${1})`, b.Config.Name) for _, cbInterface := range b.cbIndex[M] { callback := cbInterface.(*MessageCallback) Logger.Debug(`Broker:: checking callback: `, callback.ID) if callback.SlackChan != `` { if callback.SlackChan != message.Channel { Logger.Debug(`Broker:: dropping message because chan mismatch: `, callback.ID) continue //skip this message because it doesn't match the cb's channel filter } else { Logger.Debug(`Broker:: channel filter match for: `, callback.ID) } } var r *regexp.Regexp if callback.Respond { r = regexp.MustCompile(strings.Replace(botNamePat, "${1}", callback.Pattern, 1)) } else { r = regexp.MustCompile(callback.Pattern) } if r.MatchString(message.Text) { match := r.FindAllStringSubmatch(message.Text, -1)[0] Logger.Debug(`Broker:: firing callback: `, callback.ID) callback.Chan <- PatternMatch{Event: message, Match: match} } } }
func GetDeps(data []byte, dependRegex *regexp.Regexp) []string { var deps = []string{} for _, m := range dependRegex.FindAllStringSubmatch(string(data), -1) { deps = append(deps, m[regexFilepathGroupIndex]) } return deps }
// Dispatch all registered routes func (self App) ServeHTTP(res http.ResponseWriter, req *http.Request) { ctx := &Context{Req: req, Res: res} current_path := regexp.MustCompile(`/+`).ReplaceAllString("/"+strings.TrimSpace(req.URL.Path)+"/", "/") current_method := req.Method current_host := strings.TrimSpace(strings.ToLower(strings.SplitN(req.Host, ":", 2)[0])) rlen := len(self.routes) var _route route var re_vhost, re_path *regexp.Regexp var i int for i = 0; i < rlen; i++ { _route = self.routes[i] if !regexp.MustCompile("^" + _route.method + "$").MatchString(current_method) { continue } re_vhost = regexp.MustCompile("^" + _route.vhost + "$") if !re_vhost.MatchString(current_host) { continue } re_path = regexp.MustCompile("^" + _route.path + "$") if !re_path.MatchString(current_path) { continue } ctx.Params = append(re_vhost.FindAllStringSubmatch(current_host, -1)[0][1:], re_path.FindAllStringSubmatch(current_path, -1)[0][1:]...) if !_route.cb(ctx) { break } } }
// normalizeID returns an ID without the extra formatting that slack might add. // // This returns the first captured field of the first submatch using the given // precompiled regexp. If no matches are found or no captured groups are // defined then this returns the input text unchanged. func normalizeID(id string, exp *regexp.Regexp) string { idArr := exp.FindAllStringSubmatch(id, 1) if len(idArr) == 0 || len(idArr[0]) < 2 { return id } return idArr[0][1] }
func reFindAllSubmatch(re *regexp.Regexp) lua.Function { return func(l *lua.State) int { s := lua.CheckString(l, 1) n := lua.CheckInteger(l, 2) allSubmatch := re.FindAllStringSubmatch(s, n) return util.DeepPush(l, allSubmatch) } }
// parseAsx parses ENTRY nodes in the ASX playlist. func (s *Stream) parseAsx(asxp *AsxParser) []*Stream { // Regular expression to match stream URL var urlr *regexp.Regexp // First we parse all the info we can get except // the URL to a stream for _, reg := range asxRegs { // We do this one last if reg.name == "Url" { urlr = reg.reg continue } values := reg.reg.FindStringSubmatch(s.raw) var value string if len(values) == 2 { value = values[1] } else { value = asxp.getValue(reg.name) } s.setValue(reg.name, value) } // Inherit base for URLs from main playlist body if s.Base != "" { s.Base = asxp.Base } // Make sure the base ends with "/" if s.Base != "" && !strings.HasSuffix(s.Base, "/") { s.Base += "/" } // Find all the stream URLs streams := urlr.FindAllStringSubmatch(s.raw, -1) var streamsToAdd []*Stream = make([]*Stream, 0, 10) for _, stream := range streams { newStream := s.makeCopy() // Prefix base URL to the stream URL if newStream.Base != "" { stream[1] = newStream.Base + stream[1] } newStream.Url = stream[1] streamsToAdd = append(streamsToAdd, newStream) } return streamsToAdd }
func getMatchesAsMap(r *regexp.Regexp, s string) map[string]string { matches := r.FindAllStringSubmatch(s, -1)[0] names := projectRegexId.SubexpNames() md := map[string]string{} for i, n := range matches { md[names[i]] = n } return md }
func getMatch(re *regexp.Regexp, matchee *string) (match [][]string, err error) { match = nil match = re.FindAllStringSubmatch(*matchee, -1) if len(match) < 1 { err = errors.New("Could not match") log.Println(*matchee) return } return }
func (b *Brain) listener(pattern *regexp.Regexp, f ListenerFunc) ListenerFunc { return func(m adapter.Message) { results := pattern.FindAllStringSubmatch(m.Body(), -1) if len(results) > 0 { m.SetParams(results[0][1:]) log.Printf("MATCH=%s PARAMS=%s\n", pattern, m.Params()) f(m) } } }
func Params(url string, regex *regexp.Regexp, keys []string) URLParams { match := regex.FindAllStringSubmatch(url, -1)[0][1:] result := make(URLParams) for i := range match { if len(keys) <= i { break } result[keys[i]] = match[i] } return result }
// getKeywordArguments is called by the base handler to extract keyword arguments from a URL pattern. func getKeywordArguments(path string, pattern *regexp.Regexp) (args URLArgs) { args = make(URLArgs) matches := pattern.FindAllStringSubmatch(path, -1) for i, key := range pattern.SubexpNames() { if i == 0 || key == "" { continue } args[key] = matches[0][i] } return args }
// FindFirstParenStrings returns slice of first paren func FindFirstParenStrings(r *regexp.Regexp, s string) []string { captures := []string{} match := r.FindAllStringSubmatch(s, -1) if match == nil { return captures } for i := 0; i < len(match); i++ { captures = append(captures, match[i][1]) } return captures }
func checkLine(s string, r1 *regexp.Regexp, r2 *regexp.Regexp) { if r1.MatchString(s) { fmt.Println(s) res := r2.FindAllStringSubmatch(s, -1) // fmt.Println(res) if res != nil { for _, v := range res { fmt.Println("matched: ", v[1]) } } } }
// Checks if a given path matches a regex route func MatchesRoutePath(path string, re *regexp.Regexp) (bool, map[string]string) { matches := re.FindAllStringSubmatch(path, -1) attrs := make(map[string]string) if len(matches) > 0 { subExp := re.SubexpNames() for idx, exp := range subExp { attrs[exp] = matches[0][idx] } return true, attrs } return false, attrs }
func listenerFunc(pattern *regexp.Regexp, f ListenerFunc) ListenerFunc { return func(m adapter.Message) { results := pattern.FindAllStringSubmatch(m.Body(), -1) if len(results) > 0 { m.SetParams(results[0][1:]) log.Printf("TRIGGER: %s\n", pattern) log.Printf("TRIGGER: %s\n", pattern) log.Printf("PARAMS: %s\n", m.Params()) f(m) } } }
/* This is a tricky thing to parse. Right now, I keep all the * flags together and just store the offset, size, name and flags. * 00012970 l .bss 00000000 _end * 00011c60 l .init_array 00000000 __init_array_start * 00011c60 l .init_array 00000000 __preinit_array_start * 000084b0 g F .text 00000034 os_arch_start * 00000000 g .debug_aranges 00000000 __HeapBase * 00011c88 g O .data 00000008 g_os_task_list * 000082cc g F .text 0000004c os_idle_task * 000094e0 g F .text 0000002e .hidden __gnu_uldivmod_helper * 00000000 g .svc_table 00000000 SVC_Count * 000125e4 g O .bss 00000004 g_console_is_init * 00009514 g F .text 0000029c .hidden __divdi3 * 000085a8 g F .text 00000054 os_eventq_put * 00000100 O *COM* 00000004 g_idle_task_stack */ func parseObjectLine(line string, r *regexp.Regexp) (error, *symbol.SymbolInfo) { answer := r.FindAllStringSubmatch(line, 11) if len(answer) == 0 { return nil, nil } data := answer[0] if len(data) != 6 { util.StatusMessage(util.VERBOSITY_DEFAULT, "Not enough content in object file line --- %s", line) return nil, nil } si := symbol.NewSymbolInfo() si.Name = data[5] v, err := strconv.ParseUint(data[1], 16, 32) if err != nil { util.StatusMessage(util.VERBOSITY_DEFAULT, "Could not convert location from object file line --- %s", line) return nil, nil } si.Loc = int(v) v, err = strconv.ParseUint(data[4], 16, 32) if err != nil { util.StatusMessage(util.VERBOSITY_DEFAULT, "Could not convert size form object file line --- %s", line) return nil, nil } si.Size = int(v) si.Code = data[2] si.Section = data[3] /* Common section has length in a different spot. Also, these * are really global variables so mark them as such */ if si.IsSection("*COM*") { si.Size = (*si).Loc si.Code = "g" + si.Code[1:] } return nil, si }
func listTupleFilter(body string, matcher *regexp.Regexp) interface{} { var retVal [][2]string match := matcher.FindAllStringSubmatch(body, -1) if match != nil { for i := range match { if match[i] != nil && (len(match[i]) > 2) && match[i][3] != " " { retVal = append(retVal, [2]string{match[i][2], match[i][3]}) } } } return retVal }
func ParseMySQLDefaults(output string) DSN { var re *regexp.Regexp var result [][]string // Result of FindAllStringSubmatch var dsn DSN lines := strings.Split(output, "\n") for _, line := range lines { re = regexp.MustCompile("--user=(.*)") result = re.FindAllStringSubmatch(line, -1) if result != nil { dsn.Username = result[len(result)-1][1] } re = regexp.MustCompile("--password=(.*)") result = re.FindAllStringSubmatch(line, -1) if result != nil { dsn.Password = result[len(result)-1][1] } re = regexp.MustCompile("--socket=(.*)") result = re.FindAllStringSubmatch(line, -1) if result != nil { dsn.Socket = result[len(result)-1][1] } re = regexp.MustCompile("--host=(.*)") result = re.FindAllStringSubmatch(line, -1) if result != nil { dsn.Hostname = result[len(result)-1][1] } re = regexp.MustCompile("--port=(.*)") result = re.FindAllStringSubmatch(line, -1) if result != nil { dsn.Port = result[len(result)-1][1] } } if dsn.Socket != "" { // Cannot have socket & host dsn.Port = "" dsn.Hostname = "" } // Hostname always defaults to localhost. If localhost means 127.0.0.1 or socket // is handled by mysql/DSN.DSN(). if dsn.Hostname == "" && dsn.Socket == "" { dsn.Hostname = "localhost" } return dsn }
func (client *ircChatClient) Respond(L *lua.LState, pattern *regexp.Regexp, fn *lua.LFunction) { client.ircobj.AddCallback("PRIVMSG", func(e *irc.Event) { mutex.Lock() defer mutex.Unlock() matches := pattern.FindAllStringSubmatch(e.Message(), -1) mentionMe, _ := regexp.MatchString("[@:\\\\]"+client.nick+"\\s+", e.Message()) if len(matches) != 0 && mentionMe { pushN(L, fn, luar.New(L, matches[0]), luar.New(L, NewMessageEvent(e.Nick, e.Arguments[0], e.Message(), e))) if err := L.PCall(2, 0, nil); err != nil { client.ircobj.Log.Printf("[ERROR] %s", err.Error()) } } }) }
func listFilter(body string, matcher *regexp.Regexp) interface{} { var retVal []string match := matcher.FindAllStringSubmatch(body, -1) if match != nil { for i := range match { if match[i] != nil && (len(match[i]) > 1) { retVal = append(retVal, match[i][1]) } } } return retVal }
func ParseMySQLDefaults(output string) DSN { var re *regexp.Regexp var result [][]string // Result of FindAllStringSubmatch var dsn DSN // Note: Since output of mysql --print-defaults // doesn't use quotation marks for values // then we use "space" as a separator // this implies that we are unable to properly detect // e.g. passwords with spaces re = regexp.MustCompile("--user=([^ ]+)") result = re.FindAllStringSubmatch(output, -1) if result != nil { dsn.Username = result[len(result)-1][1] } re = regexp.MustCompile("--password=([^ ]+)") result = re.FindAllStringSubmatch(output, -1) if result != nil { dsn.Password = result[len(result)-1][1] } re = regexp.MustCompile("--socket=([^ ]+)") result = re.FindAllStringSubmatch(output, -1) if result != nil { dsn.Socket = result[len(result)-1][1] } if dsn.Socket == "" { re = regexp.MustCompile("--host=([^ ]+)") result = re.FindAllStringSubmatch(output, -1) if result != nil { dsn.Hostname = result[len(result)-1][1] } re = regexp.MustCompile("--port=([^ ]+)") result = re.FindAllStringSubmatch(output, -1) if result != nil { dsn.Port = result[len(result)-1][1] } } // Hostname always defaults to localhost. If localhost means 127.0.0.1 or socket // is handled by mysql/DSN.DSN(). if dsn.Hostname == "" && dsn.Socket == "" { dsn.Hostname = "localhost" } return dsn }
func (s *Server) expandTCO(body, file_type string) (body_expanded string) { //body_expanded = expand_tco_xml_re.ReplaceAllString(body, "<url>$3</url>$2<expanded_url>$3</expanded_url>") body_expanded = body var expand_tco_re *regexp.Regexp if file_type == "xml" { expand_tco_re = expand_tco_xml_re } else { expand_tco_re = expand_tco_json_re } find_pair := expand_tco_re.FindAllStringSubmatch(body_expanded, -1) for i := range find_pair { body_expanded = strings.Replace(body_expanded, find_pair[i][1], find_pair[i][2], -1) } return }
func (client *slackChatClient) Respond(L *lua.LState, pattern *regexp.Regexp, fn *lua.LFunction) { client.On(L, "message", L.NewFunction(func(L *lua.LState) int { e := L.CheckUserData(1).Value.(*slack.MessageEvent) matches := pattern.FindAllStringSubmatch(e.Text, -1) mentionMe, _ := regexp.MatchString("<@"+client.userId+"[^>]*>", e.Text) if (e.SubType == "me_message" || len(e.SubType) == 0) && len(matches) != 0 && mentionMe { user := client.userId2Name[e.User] channel := "#" + client.channelId2Name[e.Channel] pushN(L, fn, luar.New(L, matches[0]), luar.New(L, NewMessageEvent(user, channel, e.Text, e))) if err := L.PCall(2, 0, nil); err != nil { client.logger.Printf("[ERROR] %s", err.Error()) } } return 0 })) }
// regexpMatch matches the given regexp and extracts the match groups into a // named map. func regexpMatch(re *regexp.Regexp, q string) map[string]string { names := re.SubexpNames() match := re.FindAllStringSubmatch(q, -1) if len(match) == 0 { return map[string]string{} } m := map[string]string{} for i, n := range match[0] { if names[i] != "" { m[names[i]] = n } } return m }
// TailLog tails a file and sends stats to Datadog. func TailLog(t *tail.Tail, dog *statsd.Client, r *regexp.Regexp) { for line := range t.Lines { // Blank lines really mess this up - this protects against it. if line.Text == "" { continue } match := r.FindAllStringSubmatch(line.Text, -1) if match != nil { Log(fmt.Sprintf("Match: %s", match), "debug") Log(fmt.Sprintf("Sending Stat: %s", MetricName), "debug") tags := dog.Tags if MetricTag != "" { tags = append(tags, MetricTag) } dog.Count(MetricName, 1, tags, 1) } } }
func RegExHasSuffix(re *regexp.Regexp, s string) []string { if s == "" { return make([]string, 0) } re.Longest() outs := re.FindAllStringSubmatch(s, -1) newOuts := make([]string, 0) if len(outs) > 0 { inOuts := outs[0] ref := inOuts[0] for _, out := range inOuts { if out != "" && strings.HasPrefix(s, ref) && strings.HasPrefix(ref, out) { newOuts = append(newOuts, strings.TrimSpace(out)) } } } return newOuts }
func process_page(base *url.URL, page int, body string, rxp *regexp.Regexp, ctrl chan bool) { say(fmt.Sprintf("Page %d (%s) - processing regexp '%s'", page, base.String(), rxp.String())) matches := rxp.FindAllStringSubmatch(body, -1) say(fmt.Sprintf("Page %d (%s) - found %d matches", page, base.String(), len(matches))) for i := 0; i < len(matches); i++ { uri_str := matches[i][1] this_uri, err := url.Parse(uri_str) if err != nil { log.Fatalln("Error parsing url '" + uri_str + "'!") continue } if !this_uri.IsAbs() { this_uri = base.ResolveReference(this_uri) } fmt.Println(this_uri.String()) } ctrl <- true }
// Apply the regular expression and return the list of all sub-matches // and a list of the positions. The positions are unique, and calculated // doing an average of the positions of all sub-matches. func (r *Response) ReList(re *regexp.Regexp) ([][]string, []int) { matchs := re.FindAllStringSubmatch(r.Body, -1) pos := re.FindAllStringSubmatchIndex(r.Body, -1) // Merge positions into a single value (the start one) newpos := make([]int, len(pos)) for i, p := range pos { sum := 0 items := 0 for _, n := range p { sum += n items++ } newpos[i] = sum / items } return matchs, newpos }