本文整理汇总了Golang中regexp.Regexp.FindSubmatch方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.FindSubmatch方法的具体用法?Golang Regexp.FindSubmatch怎么用?Golang Regexp.FindSubmatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类regexp.Regexp
的用法示例。
在下文中一共展示了Regexp.FindSubmatch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: extractString
func extractString(src []byte, rx *regexp.Regexp) (s string) {
m := rx.FindSubmatch(src)
if m != nil {
s = strings.TrimSpace(string(m[1]))
}
return
}
示例2: checkNewChapter
func checkNewChapter(re *regexp.Regexp, l []byte) (depth int, title string) {
if m := re.FindSubmatch(l); m != nil && m[1][0] == m[3][0] {
depth = int(m[1][0] - '0')
title = string(m[2])
}
return
}
示例3: parseDeath
func parseDeath(tag, msg []byte) ([]byte, []byte) {
if bytes.Compare(tag, []byte("ActivityManager")) == 0 {
var matcher *regexp.Regexp
swap := false
if PID_KILL.Match(msg) {
matcher = PID_KILL
swap = true
} else if PID_LEAVE.Match(msg) {
matcher = PID_LEAVE
} else if PID_DEATH.Match(msg) {
matcher = PID_DEATH
}
if matcher != nil {
match := matcher.FindSubmatch(msg)
pid := match[2]
pack := match[1]
if swap {
pid = pack
pack = match[2]
}
return pid, pack
}
}
return nil, nil
}
示例4: GetWireDefaultGateway
func GetWireDefaultGateway() (net.IP, error) {
var regex *regexp.Regexp
var command *exec.Cmd
switch runtime.GOOS {
case "darwin":
command = exec.Command("netstat", "-rn", "-f", "inet")
regex = regexp.MustCompile(`default\s+(\d+\.\d+\.\d+\.\d+)\s+.*`)
case "linux":
command = exec.Command("ip", "-4", "route", "show")
regex = regexp.MustCompile(`default\s+via\s+(\d+\.\d+\.\d+\.\d+)\s+.*`)
default:
return nil, fmt.Errorf("Getting gateway is not supported in %v", runtime.GOOS)
}
output, err := command.Output()
if err != nil {
return nil, err
}
if result := regex.FindSubmatch(output); result == nil {
return nil, fmt.Errorf("Unable to get default gateway")
} else {
return net.ParseIP(string(result[1])), nil
}
}
示例5: ParseHeadID
// ParseHeadID parse ID from head by IDRegexp
func ParseHeadID(idRegexp *regexp.Regexp, head []byte) []byte {
found := idRegexp.FindSubmatch(head)
if found == nil { // not match
return head
}
return found[1]
}
示例6: processColorTemplates
func processColorTemplates(colorTemplateRegexp *regexp.Regexp, buf []byte) []byte {
// We really want ReplaceAllSubmatchFunc, i.e.: https://github.com/golang/go/issues/5690
// Instead we call FindSubmatch on each match, which means that backtracking may not be
// used in custom Regexps (matches must also match on themselves without context).
colorTemplateReplacer := func(token []byte) []byte {
tmp2 := []byte{}
groups := colorTemplateRegexp.FindSubmatch(token)
var ansiActive ActiveAnsiCodes
for _, codeBytes := range bytes.Split(groups[1], bytesComma) {
colorCode, ok := ansiColorCodes[string(codeBytes)]
if !ok {
// Don't modify the text if we don't recognize any of the codes
return groups[0]
}
for _, code := range colorCode.GetAnsiCodes() {
ansiActive.add(code)
tmp2 = append(tmp2, ansiEscapeBytes(code)...)
}
}
if len(groups[2]) > 0 {
tmp2 = append(tmp2, groups[3]...)
tmp2 = append(tmp2, ansiActive.getResetBytes()...)
}
return tmp2
}
return colorTemplateRegexp.ReplaceAllFunc(buf, colorTemplateReplacer)
}
示例7: findFirst
func findFirst(r *regexp.Regexp, body []byte) string {
matches := r.FindSubmatch(body)
if len(matches) >= 1 {
return string(matches[1])
}
return ""
}
示例8: findSubmatch
func findSubmatch(re *regexp.Regexp, b []byte) (retByte []byte) {
matches := re.FindSubmatch(b)
if len(matches) != 2 {
log.Fatal("No match")
}
retByte = matches[1]
return
}
示例9: ParsePubDate
func ParsePubDate(formatReg *regexp.Regexp, dateStr string) (time.Time, error) {
if nil == formatReg {
log.Printf("[ERROR] error parsing pubdate, date format regexp is nil")
return time.Time{}, errors.New("date format regexp is nil")
}
pubdateStr := strings.TrimSpace(dateStr)
if "" == pubdateStr {
log.Printf("[ERROR] error parsing pubdate, pubdate string is empty")
return time.Time{}, errors.New("pubdate string is empty")
}
now := time.Now()
year := now.Year()
month := int(now.Month())
day := now.Day()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
match := formatReg.FindSubmatch([]byte(pubdateStr))
if nil == match {
log.Printf("[ERROR] error parsing pubdate %s, pattern %s match failed", pubdateStr, formatReg.String())
return time.Time{}, errors.New("failed to match pubdate pattern")
}
for patInd, patName := range formatReg.SubexpNames() {
var err error
patVal := string(match[patInd])
switch patName {
case PATTERN_YEAR:
year, err = strconv.Atoi(patVal)
case PATTERN_MONTH:
month, err = ParseDateMonth(patVal)
case PATTERN_DAY:
day, err = strconv.Atoi(patVal)
case PATTERN_HOUR:
hour, err = strconv.Atoi(patVal)
case PATTERN_MINUTE:
minute, err = strconv.Atoi(patVal)
case PATTERN_SECOND:
second, err = strconv.Atoi(patVal)
}
if nil != err {
log.Printf("[ERROR] error parsing pubdate: %s, time value %s cannot be parsed: %s",
pubdateStr,
match[patInd],
err,
)
return time.Time{}, err
}
}
return time.Date(year, time.Month(month), day, hour, minute, second, 0, GOFEED_DEFAULT_TIMEZONE), nil
}
示例10: extractValue
func extractValue(s string, r *regexp.Regexp) (bool, int, error) {
matches := r.FindSubmatch([]byte(s))
if len(matches) == 2 {
val, err := strconv.ParseInt(string(matches[1]), 10, 32)
if err != nil {
return false, -1, err
}
return true, int(val), nil
}
return false, -1, nil
}
示例11: RegexpSubmatchesToMap
// Generic submatch mapper. Returns nil if no match.
func RegexpSubmatchesToMap(re *regexp.Regexp, input *[]byte) map[string]string {
submatches := re.FindSubmatch(*input)
if submatches == nil {
return nil
}
data := map[string]string{}
for i, key := range haProxyLogRe.SubexpNames() {
data[key] = string(submatches[i])
//fmt.Printf("data[%v] = \"%v\"\n", key, data[key])
}
return data
}
示例12: findSubmatch
func (s *SpecFile) findSubmatch(re *regexp.Regexp, nmatches int) ([]byte, error) {
matches := re.FindSubmatch(s.raw)
if matches != nil {
if len(matches) > nmatches {
return nil, ErrTooManySubs
} else if len(matches) < nmatches {
return nil, ErrTooFewSubs
}
} else {
return nil, ErrNoSubs
}
return matches[nmatches-1], nil
}
示例13: parseCapacity
// parseCapacity matches a Regexp in a []byte, returning the resulting value in bytes.
// Assumes that the value matched by the Regexp is in KB.
func parseCapacity(b []byte, r *regexp.Regexp) (uint64, error) {
matches := r.FindSubmatch(b)
if len(matches) != 2 {
return 0, fmt.Errorf("failed to match regexp in output: %q", string(b))
}
m, err := strconv.ParseUint(string(matches[1]), 10, 64)
if err != nil {
return 0, err
}
// Convert to bytes.
return m * 1024, err
}
示例14: parseHeadID
// ParseHeadID parse ID from head by IDRegexp
func parseHeadID(idRegexp *regexp.Regexp, head []byte) []byte {
if isUsingDefaultIDRegexp {
if i := bytes.IndexByte(head, ' '); i > 0 {
return head[0:i]
}
return head
}
found := idRegexp.FindSubmatch(head)
if found == nil { // not match
return head
}
return found[1]
}
示例15: parseDetail
func parseDetail(b []byte, detailregex *regexp.Regexp) (*Detail, error) {
detail, err := getok(detailregex.FindSubmatch(b), 1)
if err != nil {
panic("silk: failed to parse detail: " + err.Error())
}
sep := bytes.IndexAny(detail, ":=")
if sep == -1 || sep > len(detail)-1 {
return nil, errors.New("malformed detail")
}
key := clean(detail[0:sep])
return &Detail{
Key: string(bytes.TrimSpace(key)),
Value: ParseValue(detail[sep+1:]),
}, nil
}