本文整理汇总了Golang中regexp.Regexp.FindString方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.FindString方法的具体用法?Golang Regexp.FindString怎么用?Golang Regexp.FindString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类regexp.Regexp
的用法示例。
在下文中一共展示了Regexp.FindString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: find
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}
}
示例2: NewDirector
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
}
示例3: AuthorityDataExtractor
// 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
}
示例4: TestRespHeaderServedBy
// 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)
}
}
示例5: reFind
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)
}
}
示例6: doTest
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)
}
}
}
示例7: grepFile
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
}
}
示例8: ExtractAuthorityControl
// 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
}
示例9: getList
// 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
}
示例10: ifConfig
// 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
}
示例11: evaluateSQLError
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
}
示例12: RegExp
// 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)
}
}
示例13: addresses_by_frequency
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
}
示例14: loopTokens
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
}
示例15: RegexpFind
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")
}