【问题标题】:Regex: Search and replace left side = to right side正则表达式:搜索并替换左侧=右侧
【发布时间】:2023-04-10 08:45:08
【问题描述】:

如何使用正则表达式和搜索替换来转这个

[UserID] = <UserID, int,>
[UserID] = 123123
[UserID] = asd123123

into 
[UserID] = [UserID]
[UserID] = [UserID]
[UserID] = [UserID]

换句话说,我想从行的左侧获取所有内容到“=”字符,并将“=”右侧的所有内容替换为左侧的匹配项。我们可以假设每行的末尾都有一个换行符。

我的 Find what: 和 Replace with: 值是什么? (我用的是记事本++)

【问题讨论】:

    标签: regex replace


    【解决方案1】:

    这就是我在vim 中的做法:

    :%s/\[\(\w\+\)\]\s\+=\s\+.*$/[\1] = [\1]/
    

    其他的可能是它的变体,很长的一行:

    search  : \[(\w+)\]\s+=\s+.*$
    replace : [$1] = [$1]
    

    【讨论】:

      【解决方案2】:

      Notepad++ 使用 ERE(扩展正则表达式)。这是我将使用的正则表达式:

      查找:^([^\]]*]) = .*

      替换为:\1 = \1

      【讨论】:

        猜你喜欢
        • 2016-11-10
        • 1970-01-01
        • 2014-08-16
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 2019-11-03
        • 2013-06-14
        相关资源
        最近更新 更多