本文整理汇总了Golang中regexp.Regexp.ReplaceAll方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.ReplaceAll方法的具体用法?Golang Regexp.ReplaceAll怎么用?Golang Regexp.ReplaceAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类regexp.Regexp
的用法示例。
在下文中一共展示了Regexp.ReplaceAll方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: highlight
func highlight(data []byte) ([]byte, error) {
var (
re *regexp.Regexp
err error
)
for _, rule := range Rules {
if rule.Expr == "" {
continue
}
if *flDebug {
log.Printf("compile highlight re: %#v", rule)
}
re, err = regexp.Compile(rule.Expr)
if err != nil {
return nil, err
}
data = re.ReplaceAll(data, []byte(rule.Replace))
}
if *flVerbose {
if *flColorize {
log.Printf("colorize enabled")
} else {
log.Printf("colorize disabled")
}
}
if !*flColorize {
re, err = regexp.Compile(`\033\[[0-9;]*m`)
if err != nil {
return nil, err
}
data = re.ReplaceAll(data, []byte(""))
}
return data, nil
}
示例2: marshalPart
// Marshals a part of the EDIFACT. You can pass a slice callback
// to be called if a slice is found, so you can use different
// delimiters depending on certain factors.
// I don't really like passing in delimiterRegexp, but it saves
// CPU cycles. Could possibly use cache: https://github.com/pmylund/go-cache
func marshalPart(hdr Header, data reflect.Value, delimiter byte, delimiterRegexp *regexp.Regexp, sliceCallback SliceCallback) ([]byte, error) {
buf := &bytes.Buffer{}
if data.Kind() == reflect.Interface {
data = data.Elem()
}
switch data.Kind() {
default:
return []byte(""), errors.New(fmt.Sprintf("Unknown data type: %s", data.Kind()))
case reflect.String:
escapedData := delimiterRegexp.ReplaceAllString(data.String(), string(hdr.ReleaseIndicator())+"$1")
buf.WriteString(escapedData)
case reflect.Array, reflect.Slice:
// Byte slices are special. We treat them just like the string case.
if data.Type().Elem().Kind() == reflect.Uint8 {
escapedData := delimiterRegexp.ReplaceAll(data.Bytes(), []byte(string(hdr.ReleaseIndicator())+"$1"))
buf.Write(escapedData)
break
}
for n := 0; n < data.Len(); n++ {
cdata := data.Index(n)
if sliceCallback != nil {
cbBytes, err := sliceCallback(cdata)
if err != nil {
return []byte(""), err
}
buf.Write(cbBytes)
}
// we don't want to write the delimiter after the last element
if n+1 < data.Len() {
buf.WriteByte(delimiter)
}
}
}
return buf.Bytes(), nil
}
示例3: performGeneralCleanup
func (c *CssCompressor) performGeneralCleanup() {
// This function does a lot, ok?
var sb bytes.Buffer
var previousIndex int
var re *regexp.Regexp
// Remove the spaces before the things that should not have spaces before them.
// But, be careful not to turn "p :link {...}" into "p:link{...}"
// Swap out any pseudo-class colons with the token, and then swap back.
c.Css = RegexFindReplace(c.Css,
"(^|\\})(([^\\{:])+:)+([^\\{]*\\{)",
func(groups []string) string {
s := groups[0]
s = strings.Replace(s, ":", "___YUICSSMIN_PSEUDOCLASSCOLON___", -1)
s = strings.Replace(s, "\\\\", "\\\\\\\\", -1)
s = strings.Replace(s, "\\$", "\\\\\\$", -1)
return s
})
// Remove spaces before the things that should not have spaces before them.
re, _ = regexp.Compile("\\s+([!{};:>+\\(\\)\\],])")
c.Css = re.ReplaceAll(c.Css, []byte("$1"))
// Restore spaces for !important
c.Css = bytes.Replace(c.Css, []byte("!important"), []byte(" !important"), -1)
// bring back the colon
c.Css = bytes.Replace(c.Css, []byte("___YUICSSMIN_PSEUDOCLASSCOLON___"), []byte(":"), -1)
// retain space for special IE6 cases
c.Css = RegexFindReplace(c.Css,
"(?i):first\\-(line|letter)(\\{|,)",
func(groups []string) string {
return strings.ToLower(":first-"+groups[1]) + " " + groups[2]
})
// no space after the end of a preserved comment
c.Css = bytes.Replace(c.Css, []byte("*/ "), []byte("*/"), -1)
// If there are multiple @charset directives, push them to the top of the file.
c.Css = RegexFindReplace(c.Css,
"(?i)^(.*)(@charset)( \"[^\"]*\";)",
func(groups []string) string {
return strings.ToLower(groups[2]) + groups[3] + groups[1]
})
// When all @charset are at the top, remove the second and after (as they are completely ignored).
c.Css = RegexFindReplace(c.Css,
"(?i)^((\\s*)(@charset)( [^;]+;\\s*))+",
func(groups []string) string {
return groups[2] + strings.ToLower(groups[3]) + groups[4]
})
// lowercase some popular @directives
c.Css = RegexFindReplace(c.Css,
"(?i)@(charset|font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframe|media|page|namespace)",
func(groups []string) string {
return "@" + strings.ToLower(groups[1])
})
// lowercase some more common pseudo-elements
c.Css = RegexFindReplace(c.Css,
"(?i):(active|after|before|checked|disabled|empty|enabled|first-(?:child|of-type)|focus|hover|last-(?:child|of-type)|link|only-(?:child|of-type)|root|:selection|target|visited)",
func(groups []string) string {
return ":" + strings.ToLower(groups[1])
})
// lowercase some more common functions
c.Css = RegexFindReplace(c.Css,
"(?i):(lang|not|nth-child|nth-last-child|nth-last-of-type|nth-of-type|(?:-(?:moz|webkit)-)?any)\\(",
func(groups []string) string {
return ":" + strings.ToLower(groups[1]) + "("
})
// lower case some common function that can be values
// NOTE: rgb() isn't useful as we replace with #hex later, as well as and() is already done for us right after this
c.Css = RegexFindReplace(c.Css,
"(?i)([:,\\( ]\\s*)(attr|color-stop|from|rgba|to|url|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|max|min|(?:repeating-)?(?:linear|radial)-gradient)|-webkit-gradient)",
func(groups []string) string {
return groups[1] + strings.ToLower(groups[2])
})
// Put the space back in some cases, to support stuff like
// @media screen and (-webkit-min-device-pixel-ratio:0){
re, _ = regexp.Compile("(?i)\\band\\(")
c.Css = re.ReplaceAll(c.Css, []byte("and ("))
// Remove the spaces after the things that should not have spaces after them.
re, _ = regexp.Compile("([!{}:;>+\\(\\[,])\\s+")
c.Css = re.ReplaceAll(c.Css, []byte("$1"))
// remove unnecessary semicolons
re, _ = regexp.Compile(";+}")
c.Css = re.ReplaceAll(c.Css, []byte("}"))
// Replace 0(px,em,%) with 0.
re, _ = regexp.Compile("(?i)(^|[^0-9])(?:0?\\.)?0(?:px|em|%|in|cm|mm|pc|pt|ex|deg|g?rad|m?s|k?hz)")
c.Css = re.ReplaceAll(c.Css, []byte("${1}0"))
// Replace 0 0 0 0; with 0.
re, _ = regexp.Compile(":0 0 0 0(;|})")
re2, _ := regexp.Compile(":0 0 0(;|})")
//.........这里部分代码省略.........