php小编鱼仔在这篇文章中将为大家介绍regex.ReplaceAll方法的使用技巧。这个方法可以用来替换字符串中的特定字符或模式,但需要注意的是,如果替换后的字符数量与原字符串不一致,可能会导致意外结果。在使用这个方法时,我们需要注意替换字符的数量,以确保替换后的字符串与原字符串长度相同。接下来,让我们一起来了解一下具体的使用方法和技巧吧!
问题内容
我想要一个正则表达式匹配替换,它将替换的字符数与替换的字符数进行比较,而丢失的字符则用空格键代替。
我想要实现的更大目标是拥有一个带有字段和边框的模板,该模板可能会更改(因此某些字段中可用的字符数可能会有所不同),而不是对它进行硬编码,我想留下一定数量的用户接受的字符。
我的正则表达式语句:[s{0,}w{1,}s{0,}]
带有正则表达式占位符的示例模板:
| [ test1 ] | [ test2 ] | [ test3 ] |
登录后复制
现在,我想将其替换为类似的内容以保持文档结构:
___________________________________________________________________| test 1 value | test2 v.. | test3 value || | | |
登录后复制
在正则表达式替换后我得到的是,这破坏了我的文档结构
___________________________________________________________________| test 1 value | test2 value | test3 value || | | |
登录后复制
这是我的代码:
func replaceField(text string, label string, value string) string { re := regexp.MustCompile("\[\s{0,}" + label + "\s{0,}\]") return re.ReplaceAllString(t, value)}
登录后复制
你知道任何 golang 库或其他方式可以让我实现类似的目标吗?
解决方法
您可以更改正则表达式,通过将内容括在括号中来创建分组/子匹配。首先查看这里的示例, https://pkg.go.dev/regexp#示例-regexp.findstringsubmatch.
在示例输出中:
["axxxbyc" "xxx" "y"]["abzc" "" "z"]
登录后复制
我们可以看到与正则表达式匹配的整个字符串,后跟匹配的两个组中的任何一个:(x*) 和/或 (y|z)。
这是用括号括起来的原始正则表达式:
re := regexp.mustcompile("(\[\s{0,}" + label + "\s{0,}\])")
登录后复制
这是一种更简洁的写法:我对字符串文字使用反引号,因此不必使用双斜杠,并且我已将 {0,} 替换为 * 因为两者意思是“0个或多个(任何)空白”:
re := regexp.mustcompile(`([s*` + label + `s*])`)
登录后复制
现在,当我们调用 findstringsubmatch 时,我们会得到如下内容:
template := "| [ test1 ] | [ test2 ] ..."re := regexp.mustcompile(`([s*test1s*])`)fmt.printf("%q", re.findstringsubmatch(template))re = regexp.mustcompile(`([s*test2s*])`)fmt.printf("%q", re.findstringsubmatch(template))
登录后复制
["[ test1 ]" "[ test1 ]"]["[ test2 ]" "[ test2 ]"]
登录后复制
现在我们知道了与正则表达式匹配的事物的长度,我们可以使用该长度减去新事物的长度来计算需要多少个空格来填充新事物。
这是一个完整的小示例:
func main() { type field struct { num int label string } type fields []field for _, fields := range []fields{ { {1, "foo1"}, {2, "foo2"}, {3, "foo3"}, }, { {1, "foobar1"}, {2, "foobar2"}, {3, "foobar3"}, }, } { var template = ` ___________________________________________________________________ | [ test1 ] | [ test2 ] | [ test3 ] | | control 1 | control 2 | control 3 | ` for _, field := range fields { // dynamically build re label := fmt.sprintf("test%d", field.num) re := regexp.mustcompile(`([s*` + label + `s*])`) // find string that satisfies re test := re.findstringsubmatch(template)[0] // pad to len of test lentest := utf8.runecountinstring(test) lenlabel := utf8.runecountinstring(field.label) padding := strings.repeat(" ", lentest-lenlabel) final := field.label + padding // insert final label into template template = strings.replace(template, test, final, -1) } fmt.println(template) }}
登录后复制
打印:
___________________________________________________________________| Foo1 | Foo2 | Foo3 || control 1 | control 2 | control 3 |___________________________________________________________________| FooBar1 | FooBar2 | FooBar3 || control 1 | control 2 | control 3 |
登录后复制
以上就是regex.ReplaceAll 但如果替换则添加相同数量的字符的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2355336.html