正则表达式替换子匹配中的内容。

huangapple go评论98阅读模式
英文:

Regex Replace within Sub Match

问题

给定一个字符串(日志文件中的一行):

Date=2017-06-29 03:10:01.140 -700 PDT,clientDataRate="12.0,18.0,24.0,36.0,48.0,54.0",host=superawesomehost.foo,foo=bar

我想将逗号替换为一个空格,但只在双引号内部替换。

期望的结果:

Date=2017-06-29 03:10:01.140 -700 PDT,clientDataRate="12.0 18.0 24.0 36.0 48.0 54.0",host=superawesomehost.foo,foo=bar

我已经开始使用基本的正则表达式和ReplaceAllString,但很快意识到我不知道如何实现所需的匹配组(?)。

package main

import (
	"fmt"
	"log"
	"regexp"
)

func main() {
	logLine := "Date=2017-06-29 03:10:01.140 -700 PDT,clientDataRate=\"12.0,18.0,24.0,36.0,48.0,54.0\",host=superawesomehost.foo,foo=bar"
	fmt.Println("logLine:        ", logLine)
	
	reg, err := regexp.Compile(`"([^"]*)"`)
	if err != nil {
		log.Fatal(err)
	}

	repairedLogLine := reg.ReplaceAllStringFunc(logLine, func(match string) string {
		return strings.Replace(match, ",", " ", -1)
	})
	fmt.Println("repairedLogLine:", repairedLogLine)
}

非常感谢任何帮助。

英文:

Given a string (a line in a log file):

Date=2017-06-29 03:10:01.140 -700 PDT,clientDataRate="12.0,18.0,24.0,36.0,48.0,54.0",host=superawesomehost.foo,foo=bar

I'd like to replace the commas with a single space, but only within double quotes.

Desired result:

Date=2017-06-29 03:10:01.140 -700 PDT,clientDataRate="12.0 18.0 24.0 36.0 48.0 54.0",host=superawesomehost.foo,foo=bar

I've begun with a basic combination of regex and ReplaceAllString but am rapidly realizing I don't understand how to implement the match group (?) needed to accomplish this.

package main

import (
	"fmt"
	"log"
	"regexp"
)

func main() {
	logLine := "Date=2017-06-29 03:10:01.140 -700 PDT,clientDataRate=\"12.0,18.0,24.0,36.0,48.0,54.0\",host=superawesomehost.foo,foo=bar"
	fmt.Println("logLine:        ", logLine)
	
	reg, err := regexp.Compile("[^A-Za-z0-9=\"-:]+")
        if err != nil {
                log.Fatal(err)
        }

	repairedLogLine := reg.ReplaceAllString(logLine, ",")
	fmt.Println("repairedLogLine:", repairedLogLine )	
}

All help is much appreciated.

答案1

得分: 5

你需要使用Regexp.ReplaceAllStringFunc函数,它允许你将函数的结果用作子字符串的替换:

package main

import (
	"fmt"
	"log"
	"regexp"
	"strings"
)

func main() {
	logLine := `Date=2017-06-29 03:10:01.140 -700 PDT,clientDataRate="12.0,18.0,24.0,36.0,48.0,54.0",host=superawesomehost.foo,foo=bar`
	fmt.Println("logLine:        ", logLine)

	reg, err := regexp.Compile(`"([^"]*)"`)
	if err != nil {
		log.Fatal(err)
	}

	repairedLogLine := reg.ReplaceAllStringFunc(logLine, func(entry string) string {
		return strings.Replace(entry, ",", " ", -1)
	})
	fmt.Println("repairedLogLine:", repairedLogLine)
}

https://play.golang.org/p/BsZxcrrvaR

英文:

You'll want to use Regexp.ReplaceAllStringFunc, which allows you to use a function result as the replacement of a substring:

package main

import (
	"fmt"
	"log"
	"regexp"
	"strings"
)

func main() {
	logLine := `Date=2017-06-29 03:10:01.140 -700 PDT,clientDataRate="12.0,18.0,24.0,36.0,48.0,54.0",host=superawesomehost.foo,foo=bar`
	fmt.Println("logLine:        ", logLine)

	reg, err := regexp.Compile(`"([^"]*)"`)
	if err != nil {
		log.Fatal(err)
	}

	repairedLogLine := reg.ReplaceAllStringFunc(logLine, func(entry string) string {
		return strings.Replace(entry, ",", " ", -1)
	})
	fmt.Println("repairedLogLine:", repairedLogLine)
}

https://play.golang.org/p/BsZxcrrvaR

huangapple
  • 本文由 发表于 2017年8月21日 04:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/45786756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定