golang regexp replace single digits

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

golang regexp replace single digits

问题

我正在尝试替换单个数字,但仅当它们不与字母字符一起出现时才替换。例如:

[*:*:1] a2b*d 3n 4q test 5 test 6

应该变成:

[*:*:*] a2b*d 3n 4q test * test *

在上面的列表中,数字1、5和6已被替换为*,但数字2、3和4没有被替换,因为它们与字母字符一起出现。

下面的代码部分实现了部分功能:

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

s := "[*:*:1] a2b*d 3n 4q test 5 test 6"
re := regexp.MustCompile("[^0-9A-Za-z_][0-9]+[^0-9A-Za-z_]")
s = re.ReplaceAllString(s, "*")
fmt.Println(s) // 输出 - [*:** a2b*d 3n 4q test*test 6

在这里,我希望第一个单词中的:]保留下来,并保留5周围的空格。另外,6没有被替换。

有关如何在Go中实现这一点的建议吗?

英文:

I'm trying to replace single digits, only if they are not accompanied by a word character. For e.g.

[*:*:1] a2b*d 3n 4q test 5 test 6

Should be change into

[*:*:*] a2b*d 3n 4q test * test *

In the above list, the numbers 1, 5 and 6 have been replaced with *, but 2, 3 and 4 are not replaced as they are accompanied by a word character.

The below code works, but partially

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

s := "[*:*:1] a2b*d 3n 4q test 5 test 6"
re := regexp.MustCompile("[^0-9A-Za-z_][0-9]+[^0-9A-Za-z_]")
s = re.ReplaceAllString(s, "*")
fmt.Println(s) // Prints - [*:** a2b*d 3n 4q test*test 6

Here, I would like the : and the ] in the first word to be retained and the spaces around 5 to be retained. Also the 6 is not replaced.

Any suggestions on how this can be achieved in Go.

答案1

得分: 2

使用以下正则表达式:

([^\w.]|^)[0-9]([^\w.]|\.(?:\D|$)|$)

并用$1*$2替换模式进行替换。

详细说明

  • ([^\w.]|^) - 第1组:一个不是点或字母数字字符(\w = [A-Za-z0-9_])或字符串开头(^)的字符
  • [0-9] - 1个数字
  • ([^\w.]|\.(?:\D|$)|$) - 第2组:一个不是点或字母数字字符([^\w.])的字符,或者一个跟在.后面的非数字字符或字符串结尾(\.(?:\D|$)),或者字符串结尾($

替换模式中的$1将组1的内容重新插入结果中,$2将组2的内容重新插入结果中。

请参见正则表达式演示Go演示

package main

import (
	"fmt"
	"regexp"
)

func main() {
	s := "[*:*:1] a2b*d 3.5 5, 7. 3n 4q test 5 test 6"
	re := regexp.MustCompile(`([^\w.]|^)[0-9]([^\w.]|\.(?:\D|$)|$)`)
	s = re.ReplaceAllString(s, "$1*$2")
	fmt.Println(s)
}

更新:为了还删除与一个字符分隔的1位整数数字,使用循环检查是否仍然存在匹配项,并进行替换,直到没有匹配项。将s = re.ReplaceAllString(s, "$1*$2")替换为:

for re.MatchString(s)  {
	s = re.ReplaceAllString(s, "$1*$2")
}

请参见此Go演示

英文:

Use the following regex:

([^\w.]|^)[0-9]([^\w.]|\.(?:\D|$)|$)

And replace with $1*$2 replacement pattern.

Details:

  • ([^\w.]|^) - Group 1: a char that is not a dot or a word char (\w = [A-Za-z0-9_]) or start of string (^)
  • [0-9] - 1 digit
  • ([^\w.]|\.(?:\D|$)|$) - Group 2: a char other than a dot or a word char ([^\w.]), or a . followed with a non-digit or end of string (\.(?:\D|$)) or end of string ($)

The $1 in the replacement pattern re-inserts the Group 1 contents into the result, and $2 does the same with Group 2 contents.

See the regex demo and a Go demo:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	s := "[*:*:1] a2b*d 3.5 5, 7. 3n 4q test 5 test 6"
	re := regexp.MustCompile(`([^\w.]|^)[0-9]([^\w.]|\.(?:\D|$)|$)`)
	s = re.ReplaceAllString(s, "$1*$2")
	fmt.Println(s)
}

UPDATE: To also remove 1-digit integer numbers that are separated with 1 char, use a loop to check if a match still occurs, and replace until no match. Replace s = re.ReplaceAllString(s, "$1*$2") with:

for re.MatchString(s)  {
	s = re.ReplaceAllString(s, "$1*$2")
}

See this Go demo.

答案2

得分: 2

正则表达式\b\d+\b将解决你的问题。以下是Go代码。

package main

import (
    "regexp"
    "fmt"
)

func main() {
    var re = regexp.MustCompile(`\b\d+\b`)
    var str = `[*:*:1] a2b56d 3n 4q test 5 test 877565656`
    var substitution = `*`
    
    fmt.Println(re.ReplaceAllString(str, substitution))
}

你可以在https://regex101.com/r/cyCS9C/2上测试这个正则表达式。

英文:

The regex \b\d+\b will solve your problem. Following is the go code.

package main

import (
    "regexp"
    "fmt"
)

func main() {
    var re = regexp.MustCompile(`\b\d+\b`)
    var str = `[*:*:1] a2b56d 3n 4q test 5 test 877565656`
    var substitution = `*`
    
    fmt.Println(re.ReplaceAllString(str, substitution))
}

You can test this regex at https://regex101.com/r/cyCS9C/2

huangapple
  • 本文由 发表于 2016年12月1日 18:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/40908634.html
匿名

发表评论

匿名网友

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

确定