本文整理汇总了Golang中regexp.Regexp.ReplaceAllString方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.ReplaceAllString方法的具体用法?Golang Regexp.ReplaceAllString怎么用?Golang Regexp.ReplaceAllString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类regexp.Regexp
的用法示例。
在下文中一共展示了Regexp.ReplaceAllString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ParseName
func (t *ttfParser) ParseName() (err error) {
err = t.Seek("name")
if err == nil {
tableOffset, _ := t.f.Seek(0, os.SEEK_CUR)
t.rec.PostScriptName = ""
t.Skip(2) // format
count := t.ReadUShort()
stringOffset := t.ReadUShort()
for j := uint16(0); j < count && t.rec.PostScriptName == ""; j++ {
t.Skip(3 * 2) // platformID, encodingID, languageID
nameID := t.ReadUShort()
length := t.ReadUShort()
offset := t.ReadUShort()
if nameID == 6 {
// PostScript name
t.f.Seek(int64(tableOffset)+int64(stringOffset)+int64(offset), os.SEEK_SET)
var s string
s, err = t.ReadStr(int(length))
if err != nil {
return
}
s = strings.Replace(s, "\x00", "", -1)
var re *regexp.Regexp
if re, err = regexp.Compile("[(){}<> /%[\\]]"); err != nil {
return
}
t.rec.PostScriptName = re.ReplaceAllString(s, "")
}
}
if t.rec.PostScriptName == "" {
err = fmt.Errorf("the name PostScript was not found")
}
}
return
}
示例2: writeNewFile
// writeNewFile creates a new file and writes the lines to the file, stripping out the prefix if it exists.
// This will return an error if the file already exists, or if there are any errors during creation.
// the prefix will be removed if it is the first non-whitespace text in any line
func writeNewFile(name string, lines []string, prefix string) error {
out, err := createNew(name)
if err != nil {
return err
}
var reg *regexp.Regexp
if len(prefix) > 0 {
reg = regexp.MustCompile(fmt.Sprintf(`^(\s*)%s`, regexp.QuoteMeta(prefix)))
}
for _, line := range lines {
if reg != nil {
line = reg.ReplaceAllString(line, fmt.Sprintf(`$1`))
}
if _, err := out.Write([]byte(line)); err != nil {
if err2 := out.Close(); err2 != nil {
return fmt.Errorf("Error writing to and closing newfile %s: %s%s", name, err, err2)
}
return fmt.Errorf("Error writing to newfile %s: %s", name, err)
}
}
if err := out.Close(); err != nil {
return fmt.Errorf("Error closing newfile %s: %s", name, err)
}
return nil
}
示例3: replaceString
func replaceString(src string, expr string, repl string) (ret string, err error) {
var reg *regexp.Regexp
if reg, err = regexp.Compile(expr); err != nil {
return
}
ret = reg.ReplaceAllString(src, repl)
return
}
示例4: replaceAll
func replaceAll(re *regexp.Regexp, str string) string {
for {
newstr := re.ReplaceAllString(str, "")
if newstr == str {
return str
}
str = newstr
}
return str
}
示例5: replaceFirst
func replaceFirst(re *regexp.Regexp, s string, replacement string) string {
// Note that ReplaceAllStringFunc cannot be used here since it does
// not replace $1 placeholders.
loc := re.FindStringIndex(s)
if nil == loc {
return s
}
firstMatch := s[loc[0]:loc[1]]
firstMatchReplaced := re.ReplaceAllString(firstMatch, replacement)
return s[0:loc[0]] + firstMatchReplaced + s[loc[1]:]
}
示例6: Parse
// Parse sets a operand value to the register operand and
// returns the remain string and true.
//
// If the source is invalid,
// This returns the source itself and false.
// In this case doesn't change the register operand.
func (operand *Single) Parse(source string, byRegex *regexp.Regexp) (string, bool) {
matches := byRegex.FindAllString(string(source), 1)
if len(matches) <= 0 {
return source, false
}
operand.SingleValue = strings.Trim(matches[0], " \t,")
operand.HasValue = true
remains := byRegex.ReplaceAllString(string(source), "")
if strings.HasSuffix(matches[0], ",") {
remains = "," + remains
}
return remains, true
}
示例7: generateRandomString
func generateRandomString() string {
var r *regexp.Regexp
var err error
r, err = regexp.Compile(`[^\w]`)
b := make([]byte, 32)
_, err = rand.Read(b)
if err != nil {
// not sure what to do here...
log.Fatal("couldn't read random bytes...")
}
return r.ReplaceAllString(base64.StdEncoding.EncodeToString(b), "")
}
示例8: ReplacePath
/**
* 根据URL 规则,替换路径
*/
func (this *Server) ReplacePath(path string) string {
var r *regexp.Regexp
for _, reg := range this.UrlRegulars {
r, _ = regexp.Compile(reg.regular)
if r.MatchString(path) {
path = r.ReplaceAllString(path, reg.to)
break
}
}
return path
}
示例9: parsePostPhrase
// // splits the postPhrase into individual words
func parsePostPhrase(sentence string, re *regexp.Regexp) []string {
tokens := strings.Split(re.ReplaceAllString(sentence, "\u2980$1\u2980"), "\u2980")
output := []string{}
for _, token := range tokens {
// unless the token is empty
if strings.TrimSpace(token) != "" {
// unless the token is :punctuation, we prepend a space
if !re.MatchString(token) {
token = " " + token
}
output = append(output, token)
}
}
return output
}
示例10: replaceAllNamesRegex
func replaceAllNamesRegex(reg *regexp.Regexp, repl string) Option {
return func(cfg *Config) Option {
prev := cfg.nameTransform
return replaceNameTransform(func(name xml.Name) xml.Name {
if prev != nil {
name = prev(name)
}
s := reg.ReplaceAllString(name.Local, repl)
if s != name.Local {
cfg.debugf("changed %s -> %s", name.Local, s)
}
name.Local = s
return name
})(cfg)
}
}
示例11: sanitise
func sanitise(s string, strict bool) string {
var reg *regexp.Regexp
var err error
if strict {
reg, err = regexp.Compile("[^A-Za-z0-9]+")
} else {
reg, err = regexp.Compile("[^A-Za-z0-9éèàìòù]+")
}
if err != nil {
log.Fatal(err)
}
s = reg.ReplaceAllString(s, "")
s = strings.ToLower(strings.Trim(s, "-"))
return s
}
示例12: RegexpReplace
func RegexpReplace(str, replace string, regex *regexp.Regexp, count int) string {
if 0 == count {
return str
}
if regex != nil {
if count < 0 {
return regex.ReplaceAllString(str, replace)
}
return regex.ReplaceAllStringFunc(str, func(s string) string {
if count != 0 {
count -= 1
return replace
}
return s
})
}
return str
}
示例13: sanitizeName
// Returns a sanitized name based on input raw input string. By a sanitized name
// it means only alpha-numeric cachacters, all lower.
func sanitizeName(rawName string) (string, error) {
var err error
var reg *regexp.Regexp
var safe string
if reg, err = regexp.Compile("[^A-Za-z0-9]+"); err != nil {
return "", err
}
safe = reg.ReplaceAllString(rawName, "")
safe = strings.ToLower(strings.Trim(safe, ""))
if len(safe) <= 1 {
err = errors.New("Result string is too short.")
return "", err
}
return safe, nil
}
示例14: SanitizeString
// SanitizeString replaces separators with - and removes characters listed in the regexp provided from string. Accents, spaces, and all characters not in A-Za-z0-9 are replaced.
func SanitizeString(s string, r *regexp.Regexp) string {
// Remove any trailing space to avoid ending on -
s = strings.Trim(s, " ")
// Flatten accents first so that if we remove non-ascii we still get a legible name
s = RemoveAccents(s)
// Replace certain joining characters with a dash
s = separators.ReplaceAllString(s, "-")
// Remove all other unrecognised characters - NB we do allow any printable characters
s = r.ReplaceAllString(s, "")
// Remove any multiple dashes caused by replacements above
s = dashes.ReplaceAllString(s, "-")
return s
}
示例15: formatTimes
func formatTimes(timeStr string) string {
var (
re *regexp.Regexp
err error
times string
)
times = ""
if re, err = regexp.Compile("[0-9]{4}-[0-9]{2}-[0-9]{2}T"); err == nil {
times = re.ReplaceAllString(timeStr, "")
if re, err = regexp.Compile("Z"); err == nil {
times = re.ReplaceAllString(times, "")
}
}
return times
}