当前位置: 首页>>代码示例>>Golang>>正文


Golang Regexp.ReplaceAllFunc方法代码示例

本文整理汇总了Golang中regexp.Regexp.ReplaceAllFunc方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.ReplaceAllFunc方法的具体用法?Golang Regexp.ReplaceAllFunc怎么用?Golang Regexp.ReplaceAllFunc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在regexp.Regexp的用法示例。


在下文中一共展示了Regexp.ReplaceAllFunc方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: 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)
}
开发者ID:tillberg,项目名称:ansi-log,代码行数:27,代码来源:log.go

示例2: _builtinGlobal_encodeURI

func _builtinGlobal_encodeURI(call FunctionCall, characterRegexp *regexp.Regexp) Value {
	value := []byte(toString(call.Argument(0)))
	value = characterRegexp.ReplaceAllFunc(value, func(target []byte) []byte {
		// Probably a better way of doing this
		if target[0] == ' ' {
			return []byte("%20")
		}
		return []byte(url.QueryEscape(string(target)))
	})
	return toValue(string(value))
}
开发者ID:vincentshi,项目名称:otto,代码行数:11,代码来源:builtin.go

示例3: _builtinGlobal_encodeURI

func _builtinGlobal_encodeURI(call FunctionCall, escape *regexp.Regexp) Value {
	value := call.Argument(0)
	var input []uint16
	switch vl := value.value.(type) {
	case []uint16:
		input = vl
	default:
		input = utf16.Encode([]rune(toString(value)))
	}
	if len(input) == 0 {
		return toValue("")
	}
	output := []byte{}
	length := len(input)
	encode := make([]byte, 4)
	for index := 0; index < length; {
		value := input[index]
		decode := utf16.Decode(input[index : index+1])
		if value >= 0xDC00 && value <= 0xDFFF {
			panic(newURIError("URI malformed"))
		}
		if value >= 0xD800 && value <= 0xDBFF {
			index += 1
			if index >= length {
				panic(newURIError("URI malformed"))
			}
			// input = ..., value, value1, ...
			value = value
			value1 := input[index]
			if value1 < 0xDC00 || value1 > 0xDFFF {
				panic(newURIError("URI malformed"))
			}
			decode = []rune{((rune(value) - 0xD800) * 0x400) + (rune(value1) - 0xDC00) + 0x10000}
		}
		index += 1
		size := utf8.EncodeRune(encode, decode[0])
		encode := encode[0:size]
		output = append(output, encode...)
	}
	{
		value := escape.ReplaceAllFunc(output, func(target []byte) []byte {
			// Probably a better way of doing this
			if target[0] == ' ' {
				return []byte("%20")
			}
			return []byte(url.QueryEscape(string(target)))
		})
		return toValue(string(value))
	}
}
开发者ID:blendlabs,项目名称:otto,代码行数:50,代码来源:builtin.go

示例4: replaceShortcodeTokens

// Replace prefixed shortcode tokens (HUGOSHORTCODE-1, HUGOSHORTCODE-2) with the real content.
// wrapped = true means that the token has been wrapped in {@{@/@}@}
func replaceShortcodeTokens(source []byte, prefix string, wrapped bool, replacements map[string]string) (b []byte, err error) {
	var re *regexp.Regexp

	if wrapped {
		re, err = regexp.Compile(`\{@\{@` + regexp.QuoteMeta(prefix) + `-\[email protected]\}@\}`)
		if err != nil {
			return nil, err
		}
	} else {
		re, err = regexp.Compile(regexp.QuoteMeta(prefix) + `-(\d+)`)
		if err != nil {
			return nil, err
		}
	}

	// use panic/recover for reporting if an unknown
	defer func() {
		if r := recover(); r != nil {
			var ok bool
			b = nil
			err, ok = r.(error)
			if !ok {
				err = fmt.Errorf("unexpected panic during replaceShortcodeTokens: %v", r)
			}
		}
	}()
	b = re.ReplaceAllFunc(source, func(m []byte) []byte {
		key := string(m)

		if val, ok := replacements[key]; ok {
			return []byte(val)
		} else {
			panic(fmt.Errorf("unknown shortcode token %q", key))
		}
	})

	return b, err
}
开发者ID:reedobrien,项目名称:hugo,代码行数:40,代码来源:shortcode.go

示例5: replaceNonPreformattedRegexp

// As above, but further uses exp to parse the non-preformatted sections.
func replaceNonPreformattedRegexp(input []byte, exp *regexp.Regexp, replace func([]byte) []byte) []byte {
	return replaceNonPreformatted(input, func(in []byte) []byte {
		return exp.ReplaceAllFunc(in, replace)
	})
}
开发者ID:jdoliner,项目名称:kubernetes,代码行数:6,代码来源:util.go


注:本文中的regexp.Regexp.ReplaceAllFunc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。