Home  >  Article  >  Backend Development  >  regex.ReplaceAll but adds the same number of characters if replacing

regex.ReplaceAll but adds the same number of characters if replacing

王林
王林forward
2024-02-11 10:30:18801browse

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.

在示例输出中:

["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                           |

The above is the detailed content of regex.ReplaceAll but adds the same number of characters if replacing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete