通过给定输入替换不匹配的正则表达式

问题描述 投票:0回答:2

我有一个像下面的字符串。

update comment for line OBC-1234:Message is this

我想从上面的字符串中匹配OBC-1234:Message is this

我使用的正则表达式是\w*-\d+:(\w+\s?)+

我工作的工具只有一个函数,它通过一些输入参数替换匹配的正则表达式。这意味着它将首先匹配字符串中的正则表达式,并将通过给定输入替换它。但我的要求是用给定的输入替换不匹配的字符串。

输出应如下所示

update comment for line input

我知道它可以通过否定来完成,但我不知道如何将它用于更大的字符串。请帮忙。

regex regex-negation
2个回答
1
投票

你可以做:

/(.*?)(?:[A-Z\d-]+:[\w ]+)$/\1New Addition/
                     ^            words and ' ' to end of line
                  ^               literal :
            ^                     character class for OBC-1234 pattern
        ^                         Non capturing group
 ^                                Capture to the LH of description

Demo

如果OBC-1234更具体,你可以这样做:

/(.*?)(?:[A-Z]+-\d+:[\w ]+)$/\1New Addition/

更具体。

Demo 2


1
投票

使用:

  • 发现:^(.*?)\w*-\d+:\w+(?:\s+\w+)*
  • 替换:$1NEW STRING
© www.soinside.com 2019 - 2024. All rights reserved.