func main() { directory := flag.String("directory", "/usr/local/rct", "Path to the folder storing RCT experiment data") templateDirectory := flag.String("template-directory", "/usr/local/rct/server/templates", "Path to the folder storing RCT server templates (this file -> server/templates)") staticDirectory := flag.String("static-directory", "/usr/local/rct/server/static", "Path to the folder storing RCT server templates (this file -> server/static)") flag.Parse() if len(flag.Args()) > 0 { log.Fatalf("Usage: server [-directory DIRECTORY] ") } h := new(RegexpHandler) renderRoute, err := regexp.Compile("/experiments/.*/iterations/.+/.+/render") if err != nil { log.Fatal(err) } iterationRoute, err := regexp.Compile("/experiments/.*/iterations/.+/.+") if err != nil { log.Fatal(err) } staticRoute, err := regexp.Compile("/static") if err != nil { log.Fatal(err) } defaultRoute, err := regexp.Compile("/") if err != nil { log.Fatal(err) } h.HandleFunc(renderRoute, renderHandler(*directory, *templateDirectory)) h.HandleFunc(iterationRoute, newRctHandler(*directory, *templateDirectory)) h.Handler(staticRoute, http.StripPrefix("/static", http.FileServer(http.Dir(*staticDirectory)))) h.Handler(defaultRoute, http.FileServer(http.Dir(*directory))) allHandlers := jsonStripper(serverHeaderHandler(h)) log.Fatal(http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, allHandlers))) }
func Html2str(html string) string { src := string(html) //替换HTML的空白字符为空格 re := regexp.MustCompile(`\s`) //ns*r src = re.ReplaceAllString(src, " ") //将HTML标签全转换成小写 re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") src = re.ReplaceAllStringFunc(src, strings.ToLower) //去除STYLE re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>") src = re.ReplaceAllString(src, "") //去除SCRIPT re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>") src = re.ReplaceAllString(src, "") //去除所有尖括号内的HTML代码,并换成换行符 re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") src = re.ReplaceAllString(src, "\n") //去除连续的换行符 re, _ = regexp.Compile("\\s{2,}") src = re.ReplaceAllString(src, "\n") return strings.TrimSpace(src) }
func GetVersion(t *testing.T) { number_re, err := regexp.Compile("([0-9]+)") if err != nil { t.Fatal("test encountered an error") } dot_re, err := regexp.Compile("\\.") if err != nil { t.Fatal("test encountered an error") } version_from_function := getVersion() numbers := number_re.FindAllString(version_from_function, -1) dots := dot_re.FindAllString(version_from_function, -1) if len(numbers) != 3 { t.Fatal("too many version groups (should be MAJOR.MINOR.BUGFIX)") } reconstructed := []string{ numbers[0], dots[0], numbers[1], dots[1], numbers[2], dots[2], } check_string := strings.Join(reconstructed, "") if version_from_function != check_string { t.Fatal("unexpected results from version function") } }
// firewall checks that iptables does not have common firewall rules setup that would disrupt traffic func firewall() error { out, err := exec.Command("iptables", "-L", "INPUT").CombinedOutput() if err != nil { return printSuccess("Firewall Iptables Check %s: Could not run iptables", skipped) } inputRegex, err := regexp.Compile(iptablesInputRegexStr) if err != nil { // This should never happen and can only be fixed by changing the code panic(err) } if inputRegex.Match(out) { return printError("Firewall Iptables Check %s: Found INPUT rule matching %s", failed, iptablesInputRegexStr) } // Check GCE forward rules out, err = exec.Command("iptables", "-L", "FORWARD").CombinedOutput() if err != nil { return printSuccess("Firewall Iptables Check %s: Could not run iptables", skipped) } forwardRegex, err := regexp.Compile(iptablesForwardRegexStr) if err != nil { // This should never happen and can only be fixed by changing the code panic(err) } if forwardRegex.Match(out) { return printError("Firewall Iptables Check %s: Found FORWARD rule matching %s", failed, iptablesInputRegexStr) } return printSuccess("Firewall Iptables Check %s", success) }
func CleanHtml(str string, depth int) string { if depth > 0 { //将HTML标签全转换成小写 re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") str = re.ReplaceAllStringFunc(str, strings.ToLower) } if depth > 1 { //去除STYLE re, _ := regexp.Compile("\\<style[\\S\\s]+?\\</style\\>") str = re.ReplaceAllString(str, "") } if depth > 2 { //去除SCRIPT re, _ := regexp.Compile("\\<script[\\S\\s]+?\\</script\\>") str = re.ReplaceAllString(str, "") } if depth > 3 { //去除所有尖括号内的HTML代码,并换成换行符 re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") str = re.ReplaceAllString(str, "\n") } if depth > 4 { //去除连续的换行符 re, _ := regexp.Compile("\\s{2,}") str = re.ReplaceAllString(str, "\n") } return str }
// Initialize will initialize the munger func (b *BlockPath) Initialize(config *github.Config, features *features.Features) error { if len(b.Path) == 0 { glog.Fatalf("--block-path-config is required with the block-path munger") } file, err := os.Open(b.Path) if err != nil { glog.Fatalf("Failed to load block-path config: %v", err) } defer file.Close() c := &configBlockPath{} if err := yaml.NewYAMLToJSONDecoder(file).Decode(c); err != nil { glog.Fatalf("Failed to decode the block-path config: %v", err) } b.blockRegexp = []regexp.Regexp{} for _, str := range c.BlockRegexp { reg, err := regexp.Compile(str) if err != nil { return err } b.blockRegexp = append(b.blockRegexp, *reg) } b.doNotBlockRegexp = []regexp.Regexp{} for _, str := range c.DoNotBlockRegexp { reg, err := regexp.Compile(str) if err != nil { return err } b.doNotBlockRegexp = append(b.doNotBlockRegexp, *reg) } return nil }
func (i *Issue) PrettySprint() string { sa := make([]string, 0) sa = append(sa, fmt.Sprintln(i.String())) sa = append(sa, fmt.Sprintln(fmt.Sprintf("Jira URL: %s", i.Url()))) sa = append(sa, fmt.Sprintln(fmt.Sprintf("Status: %s", i.Status))) sa = append(sa, fmt.Sprintln(fmt.Sprintf("Assignee: %s", i.Assignee))) sa = append(sa, fmt.Sprintln(fmt.Sprintf("Original time estimate: %s", PrettySeconds(int(i.OriginalEstimate))))) sa = append(sa, fmt.Sprintln(fmt.Sprintf("Time spent: %s", PrettySeconds(int(i.TimeSpent))))) sa = append(sa, fmt.Sprintln(fmt.Sprintf("Remaining time estimated: %s", PrettySeconds(int(i.RemainingEstimate))))) r, _ := regexp.Compile("[*]([^*]*)[*]") splitdesc := strings.Split(i.Description, "\n") for k, v := range splitdesc { splitdesc[k] = r.ReplaceAllString(v, "\x1b[1m$1\x1b[22m") } desc := strings.Join(splitdesc, "\n") desc = strings.Replace(desc, "{color:red}", "\x1b[31m", -1) desc = strings.Replace(desc, "{color}", "\x1b[39m", -1) r2, _ := regexp.Compile("(?s)[{]quote[}](.*)[{]quote[}]") desc = r2.ReplaceAllString(desc, "\x1b[51$1\x1b[54") sa = append(sa, fmt.Sprintln(fmt.Sprintf("Description: %s", desc))) if len(i.Files) > 0 { sa = append(sa, fmt.Sprintln(fmt.Sprintf("Files: \n%v", i.Files))) } if len(i.Comments) > 0 { sa = append(sa, fmt.Sprintln(fmt.Sprintf("Comments: \n%v", i.Comments))) } if len(i.TimeLog) > 0 { sa = append(sa, fmt.Sprintln(fmt.Sprintf("Worklog: \n%v", i.TimeLog))) } return strings.Join(sa, "\n") }
func matchRegularExpressions(reader io.Reader, config HTTPProbe) bool { body, err := ioutil.ReadAll(reader) if err != nil { log.Errorf("Error reading HTTP body: %s", err) return false } for _, expression := range config.FailIfMatchesRegexp { re, err := regexp.Compile(expression) if err != nil { log.Errorf("Could not compile expression %q as regular expression: %s", expression, err) return false } if re.Match(body) { return false } } for _, expression := range config.FailIfNotMatchesRegexp { re, err := regexp.Compile(expression) if err != nil { log.Errorf("Could not compile expression %q as regular expression: %s", expression, err) return false } if !re.Match(body) { return false } } return true }
/*----------------------------------------------------------------------- | Function: WebClient Render | Description: Takes screenshot of website, can be substituted for any Tool | Inputs: | final_url - URL of the Webpage After Redirects | Returns: | filename - Filename of the Rendered Picture *-----------------------------------------------------------------------*/ func webclient_render(time_sec int, final_url string) (filename string) { // Strip Prefix on URL regex, _ := regexp.Compile("://") filename = regex.ReplaceAllString(final_url, "_") // Change (. or /) to _ dot_regex, _ := regexp.Compile("\\.|/!$|:|/") filename = dot_regex.ReplaceAllString(filename, "_") // Add .PNG File Extension and Remove / to prevent directories end_regex, _ := regexp.Compile("_?/?$") filename = end_regex.ReplaceAllString(filename, ".png") file_path := "images/" + filename timeout := strconv.Itoa(time_sec) // Execute Render Command cmd := exec.Command("/usr/bin/python", "webkit2png/scripts.py", final_url, "-o", file_path, "-t", timeout) error := cmd.Start() if error != nil { log.Fatal(error) } cmd.Wait() fmt.Println("[+] Rendered:", filename) return }
func makeTargets(lines []string, startIndex int) []Target { startRE, _ := regexp.Compile(`TargetStart, ([\d\.]+), (\d+), (\d+), (\d+)`) targetEndRE, _ := regexp.Compile(`(TargetHit|FriendHit|TargetTimeout), ([\d\.]+), ([\d\.]+), ([\d\.]+), (\d+)(, friend)?`) // get info on the three targets targetObjs := make([]Target, 3, 3) targetNumber := 0 for i := startIndex; targetNumber < 3 && i < len(lines); i++ { // set target info if match := startRE.FindStringSubmatch(lines[i]); match != nil { targetObjs[targetNumber].startTime, _ = strconv.ParseFloat(match[1], 64) targetObjs[targetNumber].ID, _ = strconv.ParseInt(match[4], 10, 64) targetObjs[targetNumber].startX, _ = strconv.ParseFloat(match[2], 64) targetObjs[targetNumber].startY, _ = strconv.ParseFloat(match[3], 64) // find end of target for j := i; j < len(lines); j++ { if match := targetEndRE.FindStringSubmatch(lines[j]); match != nil { if id, _ := strconv.ParseInt(match[5], 10, 32); id == targetObjs[targetNumber].ID { targetObjs[targetNumber].endX, _ = strconv.ParseFloat(match[3], 64) targetObjs[targetNumber].endY, _ = strconv.ParseFloat(match[4], 64) targetObjs[targetNumber].endTime, _ = strconv.ParseFloat(match[2], 64) friend := strings.HasPrefix(match[1], "Friend") || len(match[6]) > 0 targetObjs[targetNumber].enemy = !friend break } } } targetNumber++ } } return targetObjs }
func (w *resourceWrapper) Subscribe(sub *messages.Subscription) *messages.Error { compiledVerbReg, err := regexp.Compile(sub.VerbReg) if err != nil { return &messages.Error{ Message: err.Error(), Code: messages.ErrorCodeRegexp, } } compiledMethReg, err := regexp.Compile(sub.MethReg) if err != nil { return &messages.Error{ Message: err.Error(), Code: messages.ErrorCodeRegexp, } } compiledEventTypeReg, err := regexp.Compile(sub.EventTypeReg) if err != nil { return &messages.Error{ Message: err.Error(), Code: messages.ErrorCodeRegexp, } } w.router.RegisterSubscriber(w.resource, &subWrapper{ sub: sub, compiledVerbReg: compiledVerbReg, compiledMethReg: compiledMethReg, compiledEventTypeReg: compiledEventTypeReg, }) return nil }
func Fixurl(current_url, url string) string { re1, _ := regexp.Compile("http[s]?://[^/]+") destrooturl := re1.FindString(current_url) //当url为://wahaha/xiaoxixi/tupian.png if strings.HasPrefix(url, "//") { url = "http:" + url } else if strings.HasPrefix(url, "/") { // re1,_ := regexp.Compile("http[s]?://[^/]+") // destrooturl := re1.FindString(current_url) url = destrooturl + url } //当url为:"../wahaha/xiaoxixi/tupian.png"、"./wahaha/xiaoxixi/tupian.png"、"wahaha/xiaoxixi/tupian.png" if !strings.HasPrefix(url, "/") && !strings.HasPrefix(url, "http") && !strings.HasPrefix(url, "https") { // current_url = strings.TrimSuffix(current_url, "/") if destrooturl == current_url { url = current_url + "/" + url } else { re2, _ := regexp.Compile("[^/]+?$") url = re2.ReplaceAllString(current_url, "") + url } } return url }
func (self *QueryParserSuite) TestParseListSeries(c *C) { queries, err := ParseQuery("list series") c.Assert(err, IsNil) c.Assert(queries, HasLen, 1) c.Assert(queries[0].IsListQuery(), Equals, true) listSeriesQuery := queries[0].GetListSeriesQuery() c.Assert(listSeriesQuery, NotNil) c.Assert(listSeriesQuery.HasRegex(), Equals, false) c.Assert(listSeriesQuery.IncludeSpaces, Equals, false) // test the case sensitive and case insensitive list series for i := 0; i < 2; i++ { query := "list series /^foo.*/" if i == 1 { query += "i" } queries, err = ParseQuery(query) c.Assert(err, IsNil) c.Assert(queries, HasLen, 1) c.Assert(queries[0].IsListSeriesQuery(), Equals, true) listSeriesQuery = queries[0].GetListSeriesQuery() c.Assert(listSeriesQuery, NotNil) c.Assert(listSeriesQuery.HasRegex(), Equals, true) var regularExpression *regexp.Regexp if i == 1 { regularExpression, _ = regexp.Compile("(?i)^foo.*") } else { regularExpression, _ = regexp.Compile("^foo.*") } c.Assert(listSeriesQuery.GetRegex(), DeepEquals, regularExpression) c.Assert(listSeriesQuery.IncludeSpaces, Equals, false) } }
func etotal(sid string, client *http.Client, initiateData *dataproviders.InitiateData, pv *dataproviders.PvData, resp *[]byte) error { reg, err := regexp.Compile(etotalRegEx) if err != nil { log.Fail(err.Error()) return err } foundPart1 := reg.Find(*resp) if len(foundPart1) < 6 { err := fmt.Errorf("Could not find etotal in response from inverter") return err } reg, err = regexp.Compile(numberRegEx) if err != nil { log.Fail(err.Error()) } foundPart2 := reg.Find(foundPart1) if len(foundPart2) < 2 { err = fmt.Errorf("Could not find etotal in response from inverter") return err } etotal := string(foundPart2[1:]) log.Debugf("Current etotal is %s", etotal) etotalfloat, err := strconv.ParseFloat(etotal, 64) pv.EnergyTotal = float32(etotalfloat) return nil }
func compareMatch(got, expect interface{}) (int, error) { switch got := got.(type) { case []byte: switch expect := expect.(type) { case string: matcher, err := regexp.Compile(expect) if err != nil { return 0, err } return match(got, matcher) case *regexp.Regexp: return match(got, expect) } default: if got, err := toString(got); err == nil { switch expect := expect.(type) { case string: matcher, err := regexp.Compile(expect) if err != nil { return 0, err } return matchString(got, matcher) case *regexp.Regexp: return matchString(got, expect) } } else { return 0, err } } return 0, errInvalid }
// Given a set of requested names for a certificate, verifies that all of them // match the various toggles set in the role for controlling issuance. // If one does not pass, it is returned in the string argument. func validateCommonNames(req *logical.Request, commonNames []string, role *roleEntry) (string, error) { hostnameRegex, err := regexp.Compile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`) if err != nil { return "", fmt.Errorf("Error compiling hostname regex: %s", err) } subdomainRegex, err := regexp.Compile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))*$`) if err != nil { return "", fmt.Errorf("Error compiling subdomain regex: %s", err) } for _, name := range commonNames { if role.AllowLocalhost && name == "localhost" { continue } sanitizedName := name isWildcard := false if strings.HasPrefix(name, "*.") { sanitizedName = name[2:] isWildcard = true } if !hostnameRegex.MatchString(sanitizedName) { return name, nil } if role.AllowAnyName { continue } if role.AllowTokenDisplayName { if name == req.DisplayName { continue } if role.AllowSubdomains { if strings.HasSuffix(name, "."+req.DisplayName) { continue } } } if len(role.AllowedBaseDomain) != 0 { if strings.HasSuffix(name, "."+role.AllowedBaseDomain) { if role.AllowSubdomains { continue } if subdomainRegex.MatchString(strings.TrimSuffix(name, "."+role.AllowedBaseDomain)) { continue } if isWildcard && role.AllowedBaseDomain == sanitizedName { continue } } } return name, nil } return "", nil }
func main() { re1, _ := regexp.Compile("\\.[a-zA-Z]+$") s1 := re1.FindString("hello example.com") // 정규표현식으로 문자열 검색 fmt.Println(s1) // .com: 맨 마지막에 오는 ".영문"이므로 .com re2, _ := regexp.Compile("([a-zA-Z]+)(\\.[a-zA-Z]+)$") s2 := re2.FindStringSubmatch("hello example.com") // 문자열을 검색한 뒤 찾은 문자열과 // 괄호로 구분된 하위 항목 리턴 fmt.Println(s2) // [example.com example .com]: 맨 마지막에 오는 "영문.영문"을 찾고 // 하위 항목 example과 .com을 리턴 n2 := re2.FindStringSubmatchIndex("example.com") // 문자열을 검색한 뒤 찾은 문자열의 // 위치와 괄호로 구분한 하위 항목의 위치를 리턴 fmt.Println(n2) // [0 11 0 7 7 11] re3, _ := regexp.Compile("\\.[a-zA-Z.]+") s3 := re3.FindAllString("hello.co.kr world hello.net example.com", -1) // 정규표현식에 해당하는 모든 문자열 검색 fmt.Println(s3) // [.co.kr .net .com]: ".영문."에 해당하는 모든 문자열(-1) s3 = re3.FindAllString("hello.co.kr world hello.net example.com", 2) // 정규표현식에 해당하는 모든 문자열 검색 fmt.Println(s3) // [.co.kr .net]: ".영문."에 해당하는 문자열 2개 리턴 n3 := re3.FindAllStringIndex("hello.co.kr world hello.net example.com", -1) // 정규표현식에 해당하는 모든 문자열의 위치를 리턴 fmt.Println(n3) // [[5 11] [23 27] [35 39]]: ".영문."에 해당하는 모든 문자열의 위치 }
func parseSuppressions(cfg *Config) error { // Add some builtin suppressions. supp := append(cfg.Suppressions, []string{ "panic: failed to start executor binary", "panic: executor failed: pthread_create failed", "panic: failed to create temp dir", "fatal error: runtime: out of memory", "fatal error: unexpected signal during runtime execution", // presubmably OOM turned into SIGBUS "Out of memory: Kill process .* \\(syz-fuzzer\\)", "lowmemorykiller: Killing 'syz-fuzzer'", //"INFO: lockdep is turned off", // printed by some sysrq that dumps scheduler state, but also on all lockdep reports }...) for _, s := range supp { re, err := regexp.Compile(s) if err != nil { return fmt.Errorf("failed to compile suppression '%v': %v", s, err) } cfg.ParsedSuppressions = append(cfg.ParsedSuppressions, re) } for _, ignore := range cfg.Ignores { re, err := regexp.Compile(ignore) if err != nil { return fmt.Errorf("failed to compile ignore '%v': %v", ignore, err) } cfg.ParsedIgnores = append(cfg.ParsedIgnores, re) } return nil }
func (self *DataAccess) escape(name string) string { wordEx, _ := regexp.Compile(`\W`) dashEx, _ := regexp.Compile(`-+`) safe := wordEx.ReplaceAllString(name, "-") return dashEx.ReplaceAllString(strings.ToLower(safe), "-") }
func (c *CssCompressor) Compress() []byte { c.extractDataUris() c.extractComments() // preserve strings so their content doesn't get accidentally minified c.extractStrings() // strings are safe, now wrestle the comments c.processComments() // Normalize all whitespace strings to single spaces. Easier to work with that way. re, _ := regexp.Compile("\\s+") c.Css = re.ReplaceAll(c.Css, []byte(" ")) // Do lots and lots and lots of fun things c.performGeneralCleanup() // Replace multiple semi-colons in a row with a single one re, _ = regexp.Compile(";;+") c.Css = re.ReplaceAll(c.Css, []byte(";")) // restore preserved comments and strings for i, t := range c.preservedTokens { c.Css = bytes.Replace(c.Css, []byte("___YUICSSMIN_PRESERVED_TOKEN_"+strconv.Itoa(i)+"___"), []byte(t), -1) } // Trim the final string (for any leading or trailing white spaces) c.Css = bytes.TrimSpace(c.Css) // Hooray, we're done! return c.Css }
func main() { r, err := regexp.Compile(`Hello`) myRegex := regexp.MustCompile("/abc/zzz/") myString := "abc def ghi" myRegex.ReplaceAllString(myString, "$2 $1") // "def abc ghi" reg, MyErr := regexp.Compile("\\x00") if MyErr != nil { panic(MyErr) } //safe := reg.ReplaceAllString("a*- +fe 5 v90 34,j*.A E6", "") if err != nil { fmt.Printf("There is a problem with your regexp.\n") return } file, err := os.Open("DataWithNullsEveryOtherCharacter.csv") if err != nil { panic(err) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { myStr := scanner.Text() // fmt.Printf(">>[%s]\n", myStr) safe := reg.ReplaceAllString(myStr, "") fmt.Printf("%s\n", safe) }
func SlackConvertUserMentions(users []SlackUser, posts map[string][]SlackPost) map[string][]SlackPost { var regexes = make(map[string]*regexp.Regexp, len(users)) for _, user := range users { r, err := regexp.Compile("<@" + user.Id + `(\|` + user.Username + ")?>") if err != nil { l4g.Warn(utils.T("api.slackimport.slack_convert_user_mentions.compile_regexp_failed.warn"), user.Id, user.Username) continue } regexes["@"+user.Username] = r } // Special cases. regexes["@here"], _ = regexp.Compile(`<!here\|@here>`) regexes["@channel"], _ = regexp.Compile("<!channel>") regexes["@all"], _ = regexp.Compile("<!everyone>") for channelName, channelPosts := range posts { for postIdx, post := range channelPosts { for mention, r := range regexes { post.Text = r.ReplaceAllString(post.Text, mention) posts[channelName][postIdx] = post } } } return posts }
func (d *xtremIODriver) getLocalDeviceByID() (map[string]string, error) { mapDiskByID := make(map[string]string) diskIDPath := "/dev/disk/by-id" files, err := ioutil.ReadDir(diskIDPath) if err != nil { return nil, err } var match1 *regexp.Regexp var match2 string if d.r.Config.XtremIODeviceMapper || d.r.Config.XtremIOMultipath { match1, _ = regexp.Compile(`^dm-name-\w*$`) match2 = `^dm-name-\d+` } else { match1, _ = regexp.Compile(`^wwn-0x\w*$`) match2 = `^wwn-0x` } for _, f := range files { if match1.MatchString(f.Name()) { naaName := strings.Replace(f.Name(), match2, "", 1) naaName = naaName[len(naaName)-16:] devPath, _ := filepath.EvalSymlinks(fmt.Sprintf("%s/%s", diskIDPath, f.Name())) mapDiskByID[naaName] = devPath } } return mapDiskByID, nil }
func TestBuildGroup(t *testing.T) { reg1, _ := regexp.Compile(`[Le]{2}`) result1 := &CounterAction{1, 0, 0} reg2, _ := regexp.Compile(`[Spo]{3}`) result2 := &CounterAction{2, 0, 0} context := &Context{"session1", "path1"} fg := NewFunnelGroup(context) fg.addTracker(1, reg1, 100, result1) fg.addTracker(2, reg2, 100, result2) reader := strings.NewReader(`Mrs. Leonora Spocky`) var r rune = 0 var size int = 0 var err error = nil for err == nil { r, size, err = reader.ReadRune() log.Printf("Write %#v %#v %#v", r, size, err) fg.WriteRune(r, size, err) } time.Sleep(10 * time.Millisecond) if !((result1.CompleteCounter == 1) && (result1.MatchCounter == 1)) { t.Fatal("Failed Result %#v", result1) } if !((result2.CompleteCounter == 1) && (result2.MatchCounter == 1)) { t.Fatal("Failed Result %#v", result2) } }
func includeCell(f *btdpb.RowFilter, fam, col string, cell cell) bool { if f == nil { return true } // TODO(dsymonds): Implement many more filters. switch f := f.Filter.(type) { default: log.Printf("WARNING: don't know how to handle filter of type %T (ignoring it)", f) return true case *btdpb.RowFilter_FamilyNameRegexFilter: pat := string(f.FamilyNameRegexFilter) rx, err := regexp.Compile(pat) if err != nil { log.Printf("Bad family_name_regex_filter pattern %q: %v", pat, err) return false } return rx.MatchString(fam) case *btdpb.RowFilter_ColumnQualifierRegexFilter: pat := string(f.ColumnQualifierRegexFilter) rx, err := regexp.Compile(pat) if err != nil { log.Printf("Bad column_qualifier_regex_filter pattern %q: %v", pat, err) return false } return rx.MatchString(col) case *btdpb.RowFilter_ValueRegexFilter: pat := string(f.ValueRegexFilter) rx, err := regexp.Compile(pat) if err != nil { log.Printf("Bad value_regex_filter pattern %q: %v", pat, err) return false } return rx.Match(cell.value) } }
// RunScans iterates over AllScans, running each scan that matches the family // and scanner regular expressions concurrently. func (fs FamilySet) RunScans(host, ip, family, scanner string, timeout time.Duration) (map[string]FamilyResult, error) { hostname, port, err := net.SplitHostPort(host) if err != nil { hostname = host port = "443" } var addr string if net.ParseIP(ip) != nil { addr = net.JoinHostPort(ip, port) } else { addr = net.JoinHostPort(hostname, port) } familyRegexp, err := regexp.Compile(family) if err != nil { return nil, err } scannerRegexp, err := regexp.Compile(scanner) if err != nil { return nil, err } ctx := newContext(addr, hostname, familyRegexp, scannerRegexp, len(fs)) for familyName, family := range fs { familyCtx := ctx.newfamilyContext(len(family.Scanners)) for scannerName, scanner := range family.Scanners { go familyCtx.runScanner(familyName, scannerName, scanner) } } return ctx.copyResults(timeout), nil }
func (oc concreteService) GetInfo() (ntpInfo NTPInfo) { ntpPath := filepath.Join(oc.dirProvider.BaseDir(), "/bosh/log/ntpdate.out") content, err := oc.fs.ReadFileString(ntpPath) if err != nil { ntpInfo = NTPInfo{Message: "file missing"} return } lines := strings.Split(strings.Trim(content, "\n"), "\n") lastLine := lines[len(lines)-1] regex, _ := regexp.Compile(`^(.+)\s+ntpdate.+offset\s+(-*\d+\.\d+)`) badServerRegex, _ := regexp.Compile(`no server suitable for synchronization found`) matches := regex.FindAllStringSubmatch(lastLine, -1) if len(matches) > 0 && len(matches[0]) == 3 { ntpInfo = NTPInfo{ Timestamp: matches[0][1], Offset: matches[0][2], } } else if badServerRegex.MatchString(lastLine) { ntpInfo = NTPInfo{Message: "bad ntp server"} } else { ntpInfo = NTPInfo{Message: "bad file contents"} } return }
func (g Grammar) TokenREs() (res []TypedRegexp, err error) { // first get all the literals, and sort them longest first (so smaller ones don't eat larger ones). literals := map[string]bool{} g.CollectLiterals(literals) sortedLiterals := literalSorter{} for literal := range literals { sortedLiterals = append(sortedLiterals, literal) } sort.Sort(sortedLiterals) for _, literal := range sortedLiterals { re, err := regexp.Compile("^(" + regexp.QuoteMeta(literal) + ")") if err != nil { panic("regexp.QuoteMeta returned something that didn't compile") } res = append(res, TypedRegexp{"RAW", re}) } for _, symbol := range g.Symbols { var re *regexp.Regexp re, err = regexp.Compile("^" + symbol.Pattern) if err != nil { return } res = append(res, TypedRegexp{symbol.Name, re}) } return }
func (cli *CLI) init(ctx *cli.Context) error { if ctx.GlobalBool("prtg") { cli.Err, cli.Out = prtg.Err, prtg.Out } var err error if cli.cred, err = cli.Store.Load(); err == nil { cli.c, err = cli.Client(cli.cred.URL, cli.cred.User, cli.cred.Pass) } if err != nil { cli.cred = &Creds{ctx.GlobalString("addr"), ctx.GlobalString("user"), ctx.GlobalString("pass")} if cli.c, err = cli.Client(cli.cred.URL, cli.cred.User, cli.cred.Pass); err != nil { return err } } a, p, s, o := ctx.GlobalString("agent"), ctx.GlobalString("project"), ctx.GlobalString("stage"), ctx.GlobalString("output") if cli.a, err = regexp.Compile(a); err != nil { return err } if cli.p, err = regexp.Compile(p); err != nil { return err } if cli.s, err = regexp.Compile(s); err != nil { return err } if cli.o, err = regexp.Compile(o); err != nil { return err } if cli.d, err = time.ParseDuration(ctx.GlobalString("timeout")); err != nil { return err } cli.n, cli.rev = int64(ctx.GlobalInt("build")), ctx.GlobalString("revision") cli.c.SetTimeout(cli.d) return nil }
//counter query by endpoints func (this *DashBoardController) CounterQuery() { baseResp := this.BasicRespGen() _, err := this.SessionCheck() if err != nil { this.ResposeError(baseResp, err.Error()) return } endpoints := this.GetString("endpoints", "") endpointcheck, _ := regexp.Compile("^\\s*\\[\\s*\\]\\s*$") if endpoints == "" || endpointcheck.MatchString(endpoints) { this.ResposeError(baseResp, "query string is empty, please check it") return } rexstr, _ := regexp.Compile("^\\s*\\[\\s*|\\s*\\]\\s*$") endpointsArr := strings.Split(rexstr.ReplaceAllString(endpoints, ""), ",") limitNum, _ := this.GetInt("limit", 0) counters, err := dashboard.QueryCounterByEndpoints(endpointsArr, limitNum) switch { case err != nil: this.ResposeError(baseResp, err.Error()) return case len(counters) == 0: baseResp.Data["counters"] = []string{} default: baseResp.Data["counters"] = counters } this.ServeApiJson(baseResp) return }