登录
首页 >  Golang >  Go问答

以相同数量字符替换的regex.ReplaceAllWithSameAmount方法

来源:stackoverflow

时间:2024-02-07 21:51:22 428浏览 收藏

今天golang学习网给大家带来了《以相同数量字符替换的regex.ReplaceAllWithSameAmount方法》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我想要一个正则表达式匹配替换,它将替换的字符数与替换的字符数进行比较,而丢失的字符则用空格键代替。

我想要实现的更大目标是拥有一个带有字段和边框的模板,该模板可能会更改(因此某些字段中可用的字符数可能会有所不同),而不是对它进行硬编码,我想留下一定数量的用户接受的字符。

我的正则表达式语句:\[\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*test1\s*\])`)
fmt.printf("%q\n", re.findstringsubmatch(template))

re = regexp.mustcompile(`(\[\s*test2\s*\])`)
fmt.printf("%q\n", 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.ReplaceAllWithSameAmount方法》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>