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

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

How to replace multiple sub occurrences of string

问题

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

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

  1. :es1
  2. es2
  3. aaes1aa
  4. :es3,
  5. es1:
  6. ees1,
  7. ees1
  8. {
  9. "es1 :

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

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

Go程序:

  1. match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])(es1)([, | ; | : | " | ' | , | \s])`)
  2. test := `:es1
  3. es2
  4. aaes1aa
  5. :es3,
  6. es1:
  7. ees1,
  8. ees1
  9. {
  10. "es1 :`
  11. fmt.Println(match.ReplaceAllString(test, "$1es4$3"))

输出:

  1. es2
  2. aaes1aa
  3. :es3,
  4. :
  5. ees1,
  6. ees1
  7. {
  8. :

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

  1. :es4
  2. es2
  3. aaes1aa
  4. :es3,
  5. es4:
  6. ees1,
  7. ees1
  8. {
  9. "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

  1. :es1
  2. es2
  3. aaes1aa
  4. :es3,
  5. es1:
  6. ees1,
  7. ees1
  8. {
  9. "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:

  1. match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])(es1)([, | ; | : | " | ' | , | \s])`)
  2. test := `:es1
  3. es2
  4. aaes1aa
  5. :es3,
  6. es1:
  7. ees1,
  8. ees1
  9. {
  10. "es1 :`
  11. fmt.Println(match.ReplaceAllString(test, "$1es4$3"))

output:

  1. es2
  2. aaes1aa
  3. :es3,
  4. :
  5. ees1,
  6. ees1
  7. {
  8. :

I was expecting my output to be more like

  1. :es4
  2. es2
  3. aaes1aa
  4. :es3,
  5. es4:
  6. ees1,
  7. ees1
  8. {
  9. "es4 :

答案1

得分: 2

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

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])es1|^es1([, | ; | : | " | ' | , | \s])`)
  8. test := `:es1
  9. es2
  10. aaes1aa
  11. :es3,
  12. es1:
  13. ees1,
  14. ees1
  15. {
  16. "es1 :`
  17. fmt.Println(match.ReplaceAllString(test, "es4"))
  18. }

点击此处查看代码。

英文:

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

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])es1|^es1([, | ; | : | " | ' | , | \s])`)
  8. test := `:es1
  9. es2
  10. aaes1aa
  11. :es3,
  12. es1:
  13. ees1,
  14. ees1
  15. {
  16. "es1 :`
  17. fmt.Println(match.ReplaceAllString(test, "es4"))
  18. }

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

答案2

得分: 1

你可以使用以下代码:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. match := regexp.MustCompile(`([,;:'\s])es1\b|\bes1([,;:'\s])`)
  8. test := ":es1\n es2\n aaes1aa\n :es3,\n es1:\n ees1,\n ees1 \n \n {\n \"es1 :"
  9. fmt.Println(match.ReplaceAllString(test, "es4$2"))
  10. }

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

该正则表达式匹配:

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

  • | - 或者

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

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

英文:

You can use

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. match := regexp.MustCompile(`([,;:"'\\\s])es1\b|\bes1([,;:"'\\\s])`)
  8. test := ":es1\n es2\n aaes1aa\n :es3,\n es1:\n ees1,\n ees1 \n \n {\n \"es1 :"
  9. fmt.Println(match.ReplaceAllString(test, "${1}es4$2"))
  10. }

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:

确定