首页 > 后端开发 > Golang > 正文

regex.ReplaceAll 但如果替换则添加相同数量的字符

王林
发布: 2024-02-11 10:30:18
转载
1054人浏览过

regex.replaceall 但如果替换则添加相同数量的字符

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.

怪兽AI数字人
怪兽AI数字人

数字人短视频创作,数字人直播,实时驱动数字人

怪兽AI数字人 44
查看详情 怪兽AI数字人

在示例输出中:

["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 但如果替换则添加相同数量的字符的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号