本文整理汇总了Golang中regexp.Regexp.FindSubmatchIndex方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.FindSubmatchIndex方法的具体用法?Golang Regexp.FindSubmatchIndex怎么用?Golang Regexp.FindSubmatchIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类regexp.Regexp
的用法示例。
在下文中一共展示了Regexp.FindSubmatchIndex方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readLine
func readLine(data *[]byte, pattern *regexp.Regexp) ([]string, error) {
if len(*data) == 0 {
return nil, io.EOF
}
indexes := pattern.FindSubmatchIndex(*data)
if indexes == nil {
log.Println(len(*data), string((*data)[:200]))
return nil, ErrLineNotFound
} else if indexes[0] != 0 {
log.Println(indexes)
// fmt.Println(string((*data)[:indexes[len(indexes)-1]]))
nl := bytes.IndexAny(*data, "\r\n")
if nl == -1 && nl+2 < len(*data) {
return nil, ErrGarbageData
}
*data = (*data)[nl+1:]
return readLine(data, pattern)
}
parts := make([]string, len(indexes)/2-1)
for i := 2; i < len(indexes); i += 2 {
parts[i/2-1] = string((*data)[indexes[i]:indexes[i+1]])
}
*data = (*data)[indexes[1]:]
return parts, nil
}
示例2: WaitForRegexp
func (t *transportBasicIO) WaitForRegexp(re *regexp.Regexp) ([]byte, [][]byte, error) {
var matches [][]byte
out, err := t.WaitForFunc(func(buf []byte) (int, error) {
loc := re.FindSubmatchIndex(buf)
if loc != nil {
for i := 2; i < len(loc); i += 2 {
matches = append(matches, buf[loc[i]:loc[i+1]])
}
return loc[1], nil
}
return -1, nil
})
return out, matches, err
}
示例3: getDate
// getDate parses out a date from a git header (or blob with a header
// following the size and null byte). It returns the date and index
// that the unix seconds begins at within h.
func getDate(h []byte, rx *regexp.Regexp) (d date, idx int) {
m := rx.FindSubmatchIndex(h)
if m == nil {
log.Fatalf("Failed to match %s in %q", rx, h)
}
v := string(h[m[2]:m[3]])
space := strings.Index(v, " ")
if space < 0 {
log.Fatalf("unexpected date %q", v)
}
n, err := strconv.ParseInt(v[:space], 10, 64)
if err != nil {
log.Fatalf("unexpected date %q", v)
}
return date{n, v[space+1:]}, m[2]
}
示例4: replaceAll
func replaceAll(src []byte, re *regexp.Regexp, replace func(out, src []byte, m []int) []byte) []byte {
var out []byte
for len(src) > 0 {
m := re.FindSubmatchIndex(src)
if m == nil {
break
}
out = append(out, src[:m[0]]...)
out = replace(out, src, m)
src = src[m[1]:]
}
if out == nil {
return src
}
return append(out, src...)
}
示例5: findTitleMatch
// findTitleMatch returns the start of the match and the "title" subgroup of
// bytes. If the regexp doesn't match, it will return -1 and nil.
func findTitleMatch(titleRE *regexp.Regexp, input []byte) (start int, title []byte) {
indices := titleRE.FindSubmatchIndex(input)
if len(indices) == 0 {
return -1, nil
}
for i, name := range titleRE.SubexpNames() {
if name == "title" {
start, end := indices[2*i], indices[2*i+1]
return indices[0], input[start:end]
}
}
// there was no grouped named title
return -1, nil
}
示例6: checkForMatch
func (exp *Expect) checkForMatch(pat *regexp.Regexp) (m Match, found bool) {
matches := pat.FindSubmatchIndex(exp.buffer)
if matches != nil {
found = true
groupCount := len(matches) / 2
m.Groups = make([]string, groupCount)
for i := 0; i < groupCount; i++ {
start := matches[2*i]
end := matches[2*i+1]
if start >= 0 && end >= 0 {
m.Groups[i] = string(exp.buffer[start:end])
}
}
m.Before = string(exp.buffer[0:matches[0]])
exp.buffer = exp.buffer[matches[1]:]
}
return
}
示例7: checkForMatch
func (exp *Expect) checkForMatch(expr *regexp.Regexp) bool {
exp.locker.Lock()
matches := expr.FindSubmatchIndex(exp.buffer)
defer exp.locker.Unlock()
if matches != nil {
groupCount := len(matches) / 2
exp.Groups = make([]string, groupCount)
for i := 0; i < groupCount; i++ {
start := matches[2*i]
end := matches[2*i+1]
if start >= 0 && end >= 0 {
exp.Groups[i] = string(exp.buffer[start:end])
}
}
exp.Before = string(exp.buffer[0:matches[0]])
exp.buffer = exp.buffer[matches[1]:]
return true
}
return false
}
示例8: cutAllSubmatch
func cutAllSubmatch(r *regexp.Regexp, b []byte, n int) []byte {
i := r.FindSubmatchIndex(b)
return bytesCut(b, i[2*n], i[2*n+1])
}
示例9: performGeneralCleanup
//.........这里部分代码省略.........
rgbcolors := strings.Split(groups[1], ",")
var hexcolor bytes.Buffer
hexcolor.WriteString("#")
for _, colour := range rgbcolors {
val, _ := strconv.Atoi(colour)
if val < 16 {
hexcolor.WriteString("0")
}
// If someone passes an RGB value that's too big to express in two characters, round down.
// Probably should throw out a warning here, but generating valid CSS is a bigger concern.
if val > 255 {
val = 255
}
hexcolor.WriteString(fmt.Sprintf("%x", val))
}
return hexcolor.String()
})
// Shorten colors from #AABBCC to #ABC. Note that we want to make sure
// the color is not preceded by either ", " or =. Indeed, the property
// filter: chroma(color="#FFFFFF");
// would become
// filter: chroma(color="#FFF");
// which makes the filter break in IE.
// We also want to make sure we're only compressing #AABBCC patterns inside { }, not id selectors ( #FAABAC {} )
// We also want to avoid compressing invalid values (e.g. #AABBCCD to #ABCD)
sb.Reset()
re, _ = regexp.Compile("(\\=\\s*?[\"']?)?" + "#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])" + "(:?\\}|[^0-9a-fA-F{][^{]*?\\})")
previousIndex = 0
for match := re.Find(c.Css[previousIndex:]); match != nil; match = re.Find(c.Css[previousIndex:]) {
index := re.FindIndex(c.Css[previousIndex:])
submatches := re.FindStringSubmatch(string(c.Css[previousIndex:]))
submatchIndexes := re.FindSubmatchIndex(c.Css[previousIndex:])
sb.WriteString(string(c.Css[previousIndex : index[0]+len(c.Css[:previousIndex])]))
//boolean isFilter = (m.group(1) != null && !"".equals(m.group(1)));
// I hope the below is the equivalent of the above :P
isFilter := submatches[1] != "" && submatchIndexes[1] != -1
if isFilter {
// Restore, as is. Compression will break filters
sb.WriteString(submatches[1] + "#" + submatches[2] + submatches[3] + submatches[4] + submatches[5] + submatches[6] + submatches[7])
} else {
if strings.ToLower(submatches[2]) == strings.ToLower(submatches[3]) &&
strings.ToLower(submatches[4]) == strings.ToLower(submatches[5]) &&
strings.ToLower(submatches[6]) == strings.ToLower(submatches[7]) {
// #AABBCC pattern
sb.WriteString("#" + strings.ToLower(submatches[3]+submatches[5]+submatches[7]))
} else {
// Non-compressible color, restore, but lower case.
sb.WriteString("#" + strings.ToLower(submatches[2]+submatches[3]+submatches[4]+submatches[5]+submatches[6]+submatches[7]))
}
}
// The "+ 4" below is a crazy hack which will come back to haunt me later.
// For now, it makes everything work 100%.
previousIndex = submatchIndexes[7] + len(c.Css[:previousIndex]) + 4
}
if previousIndex > 0 {
sb.WriteString(string(c.Css[previousIndex:]))
}
if sb.Len() > 0 {
c.Css = sb.Bytes()