예제 #1
0
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
}
예제 #2
0
// 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)
		}
	}
}
예제 #3
0
파일: broker.go 프로젝트: ivanfoo/lazlo
//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}
		}
	}
}
예제 #4
0
파일: buildfx.go 프로젝트: buildfx/build.fx
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
}
예제 #5
0
파일: olive.go 프로젝트: leobcn/olive-go
// 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
		}
	}
}
예제 #6
0
// 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]
}
예제 #7
0
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)
	}
}
예제 #8
0
// 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
}
예제 #9
0
파일: solution.go 프로젝트: saaadhu/pandora
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
}
예제 #10
0
파일: plugin.go 프로젝트: jcline/goto
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
}
예제 #11
0
파일: brain.go 프로젝트: kvangork/victor
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)
		}
	}
}
예제 #12
0
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
}
예제 #13
0
파일: urls.go 프로젝트: arbuckle/gofw
// 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
}
예제 #14
0
파일: string.go 프로젝트: get3w/get3w
// 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
}
예제 #15
0
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])
			}
		}
	}
}
예제 #16
0
파일: routes.go 프로젝트: ThingFu/canopus
// 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
}
예제 #17
0
파일: brain.go 프로젝트: rubiojr/victor
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)
		}
	}
}
예제 #18
0
/* 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
}
예제 #19
0
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] != "&nbsp;" {
				retVal = append(retVal, [2]string{match[i][2], match[i][3]})
			}
		}
	}

	return retVal
}
예제 #20
0
파일: dsn.go 프로젝트: percona/go-mysql
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
}
예제 #21
0
파일: irc.go 프로젝트: yuin/golbot
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())
			}
		}
	})
}
예제 #22
0
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
}
예제 #23
0
파일: dsn.go 프로젝트: ZhuoRoger/go-mysql
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
}
예제 #24
0
파일: server.go 프로젝트: yanunon/go-tap
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
}
예제 #25
0
파일: slack.go 프로젝트: yuin/golbot
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
	}))
}
예제 #26
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
}
예제 #27
0
파일: tail.go 프로젝트: darron/goshe
// 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)
		}
	}
}
예제 #28
0
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
}
예제 #29
0
파일: getlinks.go 프로젝트: drbig/golang
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
}
예제 #30
0
// 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
}