本文整理汇总了Golang中github.com/etsy/hound/codesearch/regexp.Regexp.MatchString方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.MatchString方法的具体用法?Golang Regexp.MatchString怎么用?Golang Regexp.MatchString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/etsy/hound/codesearch/regexp.Regexp
的用法示例。
在下文中一共展示了Regexp.MatchString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Search
func (n *Index) Search(pat string, opt *SearchOptions) (*SearchResponse, error) {
startedAt := time.Now()
n.lck.RLock()
defer n.lck.RUnlock()
re, err := regexp.Compile(GetRegexpPattern(pat, opt.IgnoreCase))
if err != nil {
return nil, err
}
var (
g grepper
results []*FileMatch
filesOpened int
filesFound int
filesCollected int
matchesCollected int
)
var fre *regexp.Regexp
if opt.FileRegexp != "" {
fre, err = regexp.Compile(opt.FileRegexp)
if err != nil {
return nil, err
}
}
files := n.idx.PostingQuery(index.RegexpQuery(re.Syntax))
for _, file := range files {
var matches []*Match
name := n.idx.Name(file)
hasMatch := false
// reject files that do not match the file pattern
if fre != nil && fre.MatchString(name, true, true) < 0 {
continue
}
filesOpened++
if err := g.grep2File(filepath.Join(n.Ref.dir, "raw", name), re, int(opt.LinesOfContext),
func(line []byte, lineno int, before [][]byte, after [][]byte) (bool, error) {
hasMatch = true
if filesFound < opt.Offset || (opt.Limit > 0 && filesCollected >= opt.Limit) {
return false, nil
}
matchesCollected++
matches = append(matches, &Match{
Line: string(line),
LineNumber: lineno,
Before: toStrings(before),
After: toStrings(after),
})
if matchesCollected > matchLimit {
return false, fmt.Errorf("search exceeds limit on matches: %d", matchLimit)
}
return true, nil
}); err != nil {
return nil, err
}
if !hasMatch {
continue
}
filesFound++
if len(matches) > 0 {
filesCollected++
results = append(results, &FileMatch{
Filename: name,
Matches: matches,
})
}
}
return &SearchResponse{
Matches: results,
FilesWithMatch: filesFound,
FilesOpened: filesOpened,
Duration: time.Now().Sub(startedAt),
Revision: n.Ref.Rev,
}, nil
}