Notepad++ 正则表达式替换 - \1 不起作用? [英] Notepad++ Regex replace - \1 doesn't work?

查看:120
本文介绍了Notepad++ 正则表达式替换 - \1 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为序言,我已经阅读了很多关于此的问题,但我找不到答案(至少在我的挖掘中).如果我错过了,请随时指出!

Just to preface, I have read many of the questions regarding this, but I couldn't find an answer (at least in my digging). Feel free to point it out if I did miss it!

我知道如何使用正则表达式,事实上我正在寻找我要搜索的文本.但是,当我尝试按照其他问题建议的 "\1MyAppendedTextHere" 执行并替换时,它只会删除匹配的模式并添加 \1 后面的内容.(我查过的以前的问题说明 "\1" 是记事本++如何做到这一点的).这有改变吗?我做错了什么吗?

I know how to use a regex, and in fact I am finding the text I am searching for. However, when I try to do as other questions suggested "\1MyAppendedTextHere" and replace, it just erases the matched pattern and adds what follows the \1. (Previous questions I looked up stated that the "\1" was how notepad++ did this). Has this changed? Am I doing something wrong?

这是它的样子:

find: name[A-Z_0-9]+
replace: \1_SUFFIX

感谢任何帮助!

推荐答案

\1 引用了第一个 捕获组,表示搜索正则表达式中的第一组括号.您的正则表达式中没有,因此 \1 没有任何可参考的内容.

\1 references the contents of the first capturing group, which means the first set of parentheses in your search regex. There isn't one in your regex, so \1 has nothing to refer to.

如果要引用整个匹配项,请使用 \0,或者在正则表达式的相关部分周围添加括号.

Use \0 if you want to reference the entire match, or add parentheses around the relevant part of the regex.

find: name[A-Z_0-9]+
replace: \0_SUFFIX

会将nameABC 改为nameABC_SUFFIX.

使用捕获组,您可以执行以下操作

Using capturing groups, you can do things like

find: name([A-Z_0-9]+)     
replace: \1_SUFFIX

它将用 ABC_SUFFIX 替换 nameABC.

这篇关于Notepad++ 正则表达式替换 - \1 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆