func find(r *regexp.Regexp, code string, tokenType string) Token { if m := r.FindString(code); m != "" { return Token{tokenType, m, len(m), true} } return Token{"", "", 0, false} }
func NewDirector(ctx context.Context, target string, user string, password string) (*Director, error) { var re *regexp.Regexp re = regexp.MustCompile("^http(s)?://") if re.FindString(target) == "" { target = "https://" + target } re = regexp.MustCompile(":\\d+$") if re.FindString(target) == "" { target = target + ":25555" } d := gogobosh.NewDirector(target, user, password) c := &Director{ director: api.NewBoshDirectorRepository(&d, net.NewDirectorGateway()), } err := c.Connect() if err != nil { if !strings.Contains(err.Error(), "connection refused") { return nil, err } log.Printf("[DEBUG] Connection to director not successful. Could be because " + "the Director does no exist yet. So connection has been deferred.") } else { log.Printf("[DEBUG] Connection to director successful.") } return c, nil }
// authority data extraction worker func AuthorityDataExtractor(in chan *Page, out chan *string, ack chan bool, filter *regexp.Regexp, authorityDataPattern *regexp.Regexp) { var pp *Page for { // get the page pointer pp = <-in if pp == nil { break } // get the page p := *pp // do some stuff with the page p.CanonicalTitle = CanonicalizeTitle(p.Title) m := filter.MatchString(p.CanonicalTitle) if !m && p.Redir.Title == "" { // specific to category extraction result := authorityDataPattern.FindString(p.Text) if result != "" { // https://cdn.mediacru.sh/JsdjtGoLZBcR.png result = strings.Replace(result, "\t", "", -1) // fmt.Printf("%s\t%s\n", p.Title, result) line := fmt.Sprintf("%s\t%s", p.Title, result) out <- &line } } } ack <- true }
// Should set an 'Served-By' header giving information on the edge node and location served from. func TestRespHeaderServedBy(t *testing.T) { ResetBackends(backendsByPriority) var expectedServedByRegexp *regexp.Regexp var headerName string switch { case vendorCloudflare: headerName = "CF-RAY" expectedServedByRegexp = regexp.MustCompile("^[a-z0-9]{16}-[A-Z]{3}$") case vendorFastly: headerName = "X-Served-By" expectedServedByRegexp = regexp.MustCompile("^cache-[a-z0-9]+-[A-Z]{3}$") default: t.Fatal(notImplementedForVendor) } req := NewUniqueEdgeGET(t) resp := RoundTripCheckError(t, req) defer resp.Body.Close() actualHeader := resp.Header.Get(headerName) if actualHeader == "" { t.Error(headerName + " header has not been set by Edge") } if expectedServedByRegexp.FindString(actualHeader) != actualHeader { t.Errorf("%s is not as expected: got %q", headerName, actualHeader) } }
func reFind(re *regexp.Regexp) lua.Function { return func(l *lua.State) int { s := lua.CheckString(l, 1) all := re.FindString(s) return util.DeepPush(l, all) } }
func doTest(t *testing.T, name string, re *regexp.Regexp, cases []testCase) { for _, c := range cases { got := re.FindString(c.in) want := wantStr(c.in, c.want) if got != want { t.Errorf(`%s.FindString("%s") got "%s", want "%s"`, name, c.in, got, want) } } }
func grepFile(path string, pattern *regexp.Regexp) *SearchResultPerFile { result := &SearchResultPerFile{ Appearances: make([][]string, 0), } f, err := os.Open(path) defer f.Close() if err != nil { log.Errorf("Open file %s error: %s", path, err) return result } lines := make(map[int]string) lineno := 0 scanner := bufio.NewScanner(f) for scanner.Scan() { lines[lineno] = scanner.Text() lineno++ } if err := scanner.Err(); err != nil { log.Errorf("reading file %s error: %s", path, err) } linesToShow := make([]int, 0) for i := 0; i < lineno; i++ { if pattern.FindString(lines[i]) != "" { linesToShow = addToSlice(i, linesToShow) for j := 1; j <= CONTEXTS && (i-j) >= 0; j++ { linesToShow = addToSlice(i-j, linesToShow) } for j := 1; j <= CONTEXTS && (i+j) < lineno; j++ { linesToShow = addToSlice(i+j, linesToShow) } } } sort.Ints(linesToShow) last := -1 for i, number := range linesToShow { if i == 0 || (last+1) != number { result.Appearances = append(result.Appearances, []string{lines[number]}) } else { numAppearences := len(result.Appearances) result.Appearances[numAppearences-1] = append(result.Appearances[numAppearences-1], lines[number]) } last = number } if len(result.Appearances) > 0 { return result } else { return nil } }
// ExtractPageAuthorityControl extract raw authority control data from a page. func ExtractAuthorityControl(page *Page, filter, pattern *regexp.Regexp) []string { m := filter.MatchString(CanonicalizeTitle(page.Title)) var result []string if !m && page.Redir.Title == "" { match := pattern.FindString(page.Text) if match != "" { // https://cdn.mediacru.sh/JsdjtGoLZBcR.png result = append(result, page.Title, strings.Replace(match, "\t", "", -1)) } } return result }
// get a list of all services, limit to those matching the search criteria func getList(serviceString string, tag string) []*api.ServiceEntry { client, err := getClient("") if err != nil { log.Fatalf("Unable to get a consul client connection: %s\n", err) } catalog := client.Catalog() // services is a map[string] to slice of tags services, _, err := catalog.Services(nil) if err != nil { log.Fatalf("Error getting serives from catalog: %s\n", err) } // get a handle to the health endpoint and pre-calculate the regexp health := client.Health() var re *regexp.Regexp if serviceString != "" { re, err = regexp.Compile(serviceString) if err != nil { log.Fatalf("Error compiling <%s> as regexp: %s\n", serviceString, err) } } // prepare a slice to hold the result list seOut := make([]*api.ServiceEntry, 0) for service, _ := range services { match := true if re != nil { str := re.FindString(service) match = (str != "") } if match { seList, _, err := health.Service(service, tag, false, nil) if err != nil { log.Fatalf("Unable to query health status of: %s\n", err) } for _, se := range seList { seOut = append(seOut, se) } } } return seOut }
// ifConfig returns the IP Address for this node according to ifConfig, for the // specified device. func (f *NetworkFingerprint) ifConfig(device string) string { ifConfigPath, _ := exec.LookPath("ifconfig") if ifConfigPath == "" { f.logger.Println("[WARN] fingerprint.network: ifconfig not found") return "" } outBytes, err := exec.Command(ifConfigPath, device).Output() if err != nil { f.logger.Printf("[WARN] fingerprint.network: Error calling ifconfig (%s %s): %v", ifConfigPath, device, err) return "" } // Parse out the IP address returned from ifconfig for this device // Tested on Ubuntu, the matching part of ifconfig output for eth0 is like // so: // inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 // For OS X and en0, we have: // inet 192.168.0.7 netmask 0xffffff00 broadcast 192.168.0.255 output := strings.TrimSpace(string(outBytes)) // re is a regular expression, which can vary based on the OS var re *regexp.Regexp if "darwin" == runtime.GOOS { re = regexp.MustCompile("inet [0-9].+") } else { re = regexp.MustCompile("inet addr:[0-9].+") } args := strings.Split(re.FindString(output), " ") var ip string if len(args) > 1 { ip = strings.TrimPrefix(args[1], "addr:") } // validate what we've sliced out is a valid IP if net.ParseIP(ip) == nil { f.logger.Printf("[WARN] fingerprint.network: Unable to parse IP in output of '%s %s'", ifConfigPath, device) return "" } return ip }
func evaluateSQLError(err error) (error, int) { var r *regexp.Regexp matched, _ := regexp.MatchString("violates foreign key constraint", err.Error()) if matched == true { r = regexp.MustCompile("user_id|category_id|question_id") return errors.New("The provided " + r.FindString(err.Error()) + " does not exist"), http.StatusBadRequest } matched, _ = regexp.MatchString("duplicate key value violates unique constraint", err.Error()) if matched == true { r = regexp.MustCompile("username|title") return errors.New("The provided " + r.FindString(err.Error()) + " is not unique"), http.StatusConflict } return InternalErr, http.StatusInternalServerError }
// RegExp 正規表現を使用するパーサーを返す func RegExp(pattern *regexp.Regexp) Parser { return func(target string, position int) *Result { if position > len(target) { return incompatible(position) } sample := target[position:] if pattern.MatchString(sample) { index := pattern.FindStringIndex(sample) return &Result{ Success: true, Target: pattern.FindString(sample), Position: position + index[1], Attributes: map[string]string{}, } } return incompatible(position) } }
func addresses_by_frequency(msgs *notmuch.Messages, name string, pass uint, addr_to_realname *map[string]*frequencies) *frequencies { freqs := make(frequencies) pattern := `\s*(("(\.|[^"])*"|[^,])*<?(?mail\b\w+([-+.]\w+)*\@\w+[-\.\w]*\.([-\.\w]+)*\w\b)>?)` // pattern := "\\s*((\\\"(\\\\.|[^\\\\\"])*\\\"|[^,])*" + // "<?(?P<mail>\\b\\w+([-+.]\\w+)*\\@\\w+[-\\.\\w]*\\.([-\\.\\w]+)*\\w\\b)>?)" pattern = `.*` + strings.ToLower(name) + `.*` var re *regexp.Regexp = nil var err os.Error = nil if re, err = regexp.Compile(pattern); err != nil { log.Printf("error: %v\n", err) return &freqs } headers := []string{"from"} if pass == 1 { headers = append(headers, "to", "cc", "bcc") } for ; msgs.Valid(); msgs.MoveToNext() { msg := msgs.Get() //println("==> msg [", msg.GetMessageId(), "]") for _, header := range headers { froms := strings.ToLower(msg.GetHeader(header)) //println(" froms: ["+froms+"]") for _, from := range strings.Split(froms, ",", -1) { from = strings.Trim(from, " ") match := re.FindString(from) //println(" -> match: ["+match+"]") occ, ok := freqs[match] if !ok { freqs[match] = 0 occ = 0 } freqs[match] = occ + 1 } } } return &freqs }
func loopTokens( spMap count.TokensMap, compiledTokenRE *regexp.Regexp, allTokens AllTokens, tokenType int, ) []string { tokens := []string{} filter, total := chopUpChannelFilterFlag() for token := range spMap { token = compiledTokenRE.FindString(token) if total == 0 { tokens = append(tokens, token) // add to deduped list allTokens[token] = tokenType } else { if filter[token] == true { tokens = append(tokens, token) // add to deduped list allTokens[token] = tokenType } } } return tokens }
func RegexpFind(env *Glisp, name string, args []Sexp) (Sexp, error) { if len(args) != 2 { return SexpNull, WrongNargs } var haystack string switch t := args[1].(type) { case SexpStr: haystack = string(t) default: return SexpNull, errors.New(fmt.Sprintf("2nd argument of %v should be a string", name)) } var needle regexp.Regexp switch t := args[0].(type) { case SexpRegexp: needle = regexp.Regexp(t) default: return SexpNull, errors.New(fmt.Sprintf("1st argument of %v should be a compiled regular expression", name)) } switch name { case "regexp-find": str := needle.FindString(haystack) return SexpStr(str), nil case "regexp-find-index": return regexpFindIndex(needle, haystack) case "regexp-match": matches := needle.MatchString(haystack) return SexpBool(matches), nil } return SexpNull, errors.New("unknown function") }
// findRegexp returns the first string from current scanning position that matches given regular expression func (l *Lexer) findRegexp(r *regexp.Regexp) string { return r.FindString(l.input[l.pos:]) }
func (p NumericParser) parse(s string) (*Numeric, error) { var ( n *Numeric err error sign string reStr string re *regexp.Regexp parseErr = errors.New(ParseNumericError) ) // Record whether the input string has a currency symbol. // If so, it can only be a monetary value. hasCurrency := p.currencyRegex.MatchString(s) if hasCurrency { s = p.removeCurrencySymbol(s) } // Now determine whether the string's initial character is a + or -. // If so, strip it away and record the sign. sign = "" re = regexp.MustCompile("^[\\+-]") if re.MatchString(s) { if re.FindString(s) == "-" { sign = "-" } s = s[1:] } // Since currency and sign symbols have been stripped, we now check that the // expression begins with a decimal separator (possibly) and digit. // Valid strings thus look like either: .x* or x*. reStr = "^" + p.decimalReStr + "?" + "[0-9]" re = regexp.MustCompile(reStr) if !re.MatchString(s) { return nil, parseErr } // Prepend a 0 if the string begins with a decimal separator. reStr = "^" + p.decimalReStr re = regexp.MustCompile(reStr) if re.MatchString(s) { s = "0" + s } // If the input ends with the decimal separator, remove it. re = regexp.MustCompile(p.decimalReStr + "$") if re.MatchString(s) { s = re.ReplaceAllString(s, "") } // Create the main validating regex. reStr = "^\\d+" + "(" + p.digitReStr + "\\d{3})*" + p.decimalReStr + "?\\d*$" re = regexp.MustCompile(reStr) if !re.MatchString(s) { return nil, parseErr } // We can now assume that the string is valid except for // intermediate delimiters. // Before attempting to parse the string further, we (possibly) perform // some basic sanitization. var parsed string tmp, err := p.sanitize(s) if err == nil { parsed = tmp } else { // Probably the parser cannot distinguish between decimal and digit // separators. So we handle this case separately. re = regexp.MustCompile(p.digitReStr + "|" + p.decimalReStr) locs := re.FindAllStringSubmatchIndex(s, -1) switch len(locs) { case 0: // The number is an integer. No additional parsing needed. parsed = s err = nil case 1: // Need to deal with 1,234 vs 123,456 vs 12.345, etc. parsed, err = p.parseOneUnknownSeparator(s, locs[0][0]) default: // Try to find the last separator and determine its type. parsed, err = p.parseManyUnknownSeparators(s, locs) } } parsed = sign + parsed f, ferr := strconv.ParseFloat(parsed, 64) if err != nil || ferr != nil { return nil, err } // We now know that the parsed string correctly parses as a float. n = &Numeric{ isFloat: true, f: f, } if hasCurrency { n.isMoney = true } _, err = strconv.Atoi(parsed) if err == nil { n.isInt = true } return n, nil }
func downloadcmd(opt Options) error { user := nvls(opt.Download.User, EnvUser) repo := nvls(opt.Download.Repo, EnvRepo) token := nvls(opt.Download.Token, EnvToken) tag := opt.Download.Tag name := opt.Download.Name latest := opt.Download.Latest regexed := opt.Download.Regexed vprintln("downloading...") if err := ValidateTarget(user, repo, tag, latest); err != nil { return err } // Find the release corresponding to the entered tag, if any. var rel *Release var err error if latest { rel, err = LatestRelease(user, repo, token) } else { rel, err = ReleaseOfTag(user, repo, tag, token) } if err != nil { return err } assetId := 0 var rx *regexp.Regexp var theName string if regexed { rx = regexp.MustCompile(name) } for _, asset := range rel.Assets { if regexed { theName = rx.FindString(asset.Name) } else { theName = name } if asset.Name == theName { assetId = asset.Id } } if assetId == 0 { return fmt.Errorf("coud not find asset named %s", theName) } var resp *http.Response var url string if token == "" { url = GH_URL + fmt.Sprintf("/%s/%s/releases/download/%s/%s", user, repo, tag, theName) resp, err = http.Get(url) } else { url = ApiURL() + fmt.Sprintf(ASSET_DOWNLOAD_URI, user, repo, assetId) resp, err = DoAuthRequest("GET", url, "", token, map[string]string{ "Accept": "application/octet-stream", }, nil) } if resp != nil { defer resp.Body.Close() } if err != nil { return fmt.Errorf("could not fetch releases, %v", err) } vprintln("GET", url, "->", resp) contentLength, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64) if err != nil { return err } if resp.StatusCode != http.StatusOK { return fmt.Errorf("github did not respond with 200 OK but with %v", resp.Status) } out, err := os.Create(theName) if err != nil { return fmt.Errorf("could not create file %s", theName) } defer out.Close() n, err := io.Copy(out, resp.Body) if n != contentLength { return fmt.Errorf("downloaded data did not match content length %d != %d", contentLength, n) } return err }
// Делает подстановки определений на места макровызовов. // input должен содержать одинаковые макровызовы! // FIXME: сделать input string func (def defines) substitute(input []string, re *regexp.Regexp) []string { cached := input[0] level := 0 for { // выходные значения на каждой итерации result := make([]string, 0, 16) f := false for _, str := range input { // поиск макровызова macroCall := re.FindString(str) if len(macroCall) != 0 { f = true } else { continue } // извлечение имени name := macroCall[2 : len(macroCall)-1] // проверка префикса basePath := false if name[0] == '/' { basePath = true name = name[1:] } // поиск значения values, exists := def[name] if !exists { throw("Macro definition with name %s not found", name) } // применение модификатора if basePath { values = basePathModif(values) } // подстановка каждым значением макроопределения for _, val := range values { subs := strings.Replace(str, macroCall, val, 1) result = append(result, subs) } } if !f { // expand enveronment for i, _ := range input { input[i] = expandEnvVars(input[i]) } if len(input) == 0 || cached != input[0] { log.Printf("macro L%d: [%s] -> %v", level, cached, input) } return input } input = result level++ if level > macroLevel { throw("cyclic reference in macro-call %s or depends", cached) } } return input }
func getOriginalName(text string, regexPattern *regexp.Regexp) string { reversed := Reverse(text) result := regexPattern.FindString(reversed) return Reverse(result)[1 : len(result)-4] }