在字符串之间扩展字符串
Extend string between strings
startABCend
->
startABC123end
我试图捕获 start
和 end
之间的文本并扩展它,如图所示。我试过了:
find = start.*end
、replace = 123
:会捕获start
和end
之间,但将它们全部替换掉
find = (?s)(?<=start).+?(?=end)
、replace = 123
:将保留 start
和 end
但替换捕获的
如何在 N++ 中使用正则表达式完成此操作?
确切的用例是
func_name(a, b=1) -> func_name(a, b=1, c=2)
# can also be
func_name(g=5, k=7) -> func_name(g=5, k=7, c=2)
# so capture between `func_name(` and `)` and extend with `, c=2`
您的示例目标不包括替换字符串中的白色 space。要使用组并附加数字,您可以使用方括号。
基本上:
Find: (?<=start)(.+?)(?=end)
Replace: ()123
or just
Find: start(.+?)end
Replace: start()123end
您可以在没有捕获组的情况下执行此操作,并匹配您要替换的内容。
\bstart\K.*?(?=end\b)
模式匹配:
\bstart
匹配 start
前面有单词边界
\K
忘记匹配到现在的是什么
.*?
尽可能匹配最少的字符
(?=end\b)
正面前瞻,断言 end
向右后跟单词边界
在替换中使用完整匹配后跟 123
$&123
对于更新的示例数据,您可以将 key
的格式与可选的 =value
相匹配,并可选择重复断言右侧的 )
。
\bfunc_name\([^\s,=]+(?:=[^\s,=]+)?(?:,\h*[^\s,=]+(?:=[^\s,=]+)?)*(?=\))
并替换为
$&, c=2
startABCend
->
startABC123end
我试图捕获 start
和 end
之间的文本并扩展它,如图所示。我试过了:
find = start.*end
、replace = 123
:会捕获start
和end
之间,但将它们全部替换掉find = (?s)(?<=start).+?(?=end)
、replace = 123
:将保留start
和end
但替换捕获的
如何在 N++ 中使用正则表达式完成此操作?
确切的用例是
func_name(a, b=1) -> func_name(a, b=1, c=2)
# can also be
func_name(g=5, k=7) -> func_name(g=5, k=7, c=2)
# so capture between `func_name(` and `)` and extend with `, c=2`
您的示例目标不包括替换字符串中的白色 space。要使用组并附加数字,您可以使用方括号。
基本上:
Find: (?<=start)(.+?)(?=end)
Replace: ()123
or just
Find: start(.+?)end
Replace: start()123end
您可以在没有捕获组的情况下执行此操作,并匹配您要替换的内容。
\bstart\K.*?(?=end\b)
模式匹配:
\bstart
匹配start
前面有单词边界\K
忘记匹配到现在的是什么.*?
尽可能匹配最少的字符(?=end\b)
正面前瞻,断言end
向右后跟单词边界
在替换中使用完整匹配后跟 123
$&123
对于更新的示例数据,您可以将 key
的格式与可选的 =value
相匹配,并可选择重复断言右侧的 )
。
\bfunc_name\([^\s,=]+(?:=[^\s,=]+)?(?:,\h*[^\s,=]+(?:=[^\s,=]+)?)*(?=\))
并替换为
$&, c=2