如何替换字符串的多个子出现次数

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

How to replace multiple sub occurrences of string

问题

我有一个使用情况,其中我可以拥有一个包含任何内容的文本字符串。我想要实现的目标是替换给定字符串中的某个特定模式。

假设我有一个给定的字符串:

:es1
es2
aaes1aa
:es3,
es1:
ees1,
ees1 

{
"es1 :

我在这里尝试的是,假设我必须替换这个字符串中的所有es1,但有一个条件。它必须以[, | ; | : | " | ' | \\ | \s]结尾或开头。:es1es1,es1:等都是可以接受的,但eees1sss不是。

我尝试了类似于这样的正则表达式:([, | ; | : | " | ' | \\ | \s])(es1)([, | ; | : | " | ' | , | \s]),但我不认为这是我需要的。

Go程序:

match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])(es1)([, | ; | : | " | ' | , | \s])`)
test := `:es1
es2
aaes1aa
:es3,
es1:
ees1,
ees1 

{
"es1 :`

fmt.Println(match.ReplaceAllString(test, "$1es4$3"))

输出:

es2
aaes1aa
:es3,
:
ees1,
ees1 

{
 :

我原本期望的输出更像是:

:es4
es2
aaes1aa
:es3,
es4:
ees1,
ees1 

{
"es4 :
英文:

I have one use case where I can have a text string which can contain anything. What I want to achieve is to replace a certain pattern within that given string.

Let's say I have given string as

:es1
es2
aaes1aa
:es3,
es1:
ees1,
ees1 

{
"es1 :

What I am trying to do is here is suppose I have to replace all es1 in this string but with one condition. It has to be either end or start with [, | ; | : | " | ' | \\ | \s]. :es1, es1,, es1: and so on are accepted but eees1sss is not.

I tried ([, | ; | : | " | ' | \\ | \s])(es1)([, | ; | : | " | ' | , | \s]) something like this but I don't think it's what I need.

Go program:

match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])(es1)([, | ; | : | " | ' | , | \s])`)
	test := `:es1
	es2
	aaes1aa
	:es3,
	es1:
	ees1,
	ees1 
	
	{
	"es1 :`
	
	fmt.Println(match.ReplaceAllString(test, "$1es4$3"))

output:

       es2
        aaes1aa
        :es3,
:
        ees1,
        ees1 

        {
         :

I was expecting my output to be more like

:es4
    es2
    aaes1aa
    :es3,
    es4:
    ees1,
    ees1 

    {
    "es4 :

答案1

得分: 2

下面提供的解决方案没有经过全面测试,但似乎可以工作。

package main

import (
	"fmt"
	"regexp"
)

func main() {
	match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])es1|^es1([, | ; | : | " | ' | , | \s])`)
	test := `:es1
    es2
    aaes1aa
    :es3,
    es1:
    ees1,
    ees1 
    
    {
    "es1 :`

	fmt.Println(match.ReplaceAllString(test, "es4"))
}

点击此处查看代码。

英文:

the solution provided below is not well tested against all possibilities, but it seems to be working.

package main

import (
	"fmt"
	"regexp"
)

func main() {
	match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])es1|^es1([, | ; | : | " | ' | , | \s])`)
	test := `:es1
    es2
    aaes1aa
    :es3,
    es1:
    ees1,
    ees1 
    
    {
    "es1 :`

	fmt.Println(match.ReplaceAllString(test, "es4"))
}

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

答案2

得分: 1

你可以使用以下代码:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	match := regexp.MustCompile(`([,;:'\s])es1\b|\bes1([,;:'\s])`)
	test := ":es1\n    es2\n    aaes1aa\n    :es3,\n    es1:\n    ees1,\n    ees1 \n    \n    {\n    \"es1 :"
	fmt.Println(match.ReplaceAllString(test, "es4$2"))
}

请参考Go演示正则表达式演示。请注意,方括号内的空格和|字符是有意义的,并且会与这些字符完全匹配,因此您需要从模式中删除它们。

该正则表达式匹配:

  • ([,;:'\s])es1\b - 第1组:逗号、分号、冒号、双引号、单引号、反斜杠或空格;然后是作为一个完整单词的es1\b是一个单词边界)

  • | - 或者

  • \bes1 - 一个完整的单词es1

  • ([,;:'\s]) - 第2组:逗号、分号、冒号、双引号、单引号、反斜杠或空格

英文:

You can use

package main

import (
	"fmt"
	"regexp"
)

func main() {
	match := regexp.MustCompile(`([,;:"'\\\s])es1\b|\bes1([,;:"'\\\s])`)
	test := ":es1\n    es2\n    aaes1aa\n    :es3,\n    es1:\n    ees1,\n    ees1 \n    \n    {\n    \"es1 :"
	fmt.Println(match.ReplaceAllString(test, "${1}es4$2"))
}

See the Go demo and the regex demo. Note that the spaces and | chars inside square brackets are meaningful and match these chars literally, thus, you need to remove them all from your pattern.

The regex matches:

  • ([,;:"'\\\s])es1\b - Group 1: a comma, or a semi-colon, colon, double or single quotation mark, backslash or whitespace; then es1 as a whole word (\b is a word boundary)
  • | - or
  • \bes1 - a whole word es1
  • ([,;:"'\\\s]) - Group 2: a comma, or a semi-colon, colon, double or single quotation mark, backslash or whitespace

huangapple
  • 本文由 发表于 2021年9月2日 16:03:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/69026377.html
匿名

发表评论

匿名网友

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

确定