Notepad ++ Regex反向引用语法在Search / Replace - \1或$ 1中 [英] Notepad++ Regex Backreference syntax in Search/Replace - \1 or $1

查看:540
本文介绍了Notepad ++ Regex反向引用语法在Search / Replace - \1或$ 1中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试使用Notepad ++搜索/替换与正则表达式替换这些单词的较短版本的特定单词。






我使用以下正则表达式来匹配以(但不是作为一个单词结尾的每个单词) - 并使用相关的引用替换相同单词减去结尾 r 的匹配单词:



找到什么: ([a-zA-z] + e)r / strong>



替换为: $ 1



但是,即使找到匹配的单词也不会替换匹配的单词。



但是,如果我将反向引用语法更改为:



替换为: \1






为什么 $ 1 backreference工作?



两种形式之间的区别 - \1 $ 1

解决方案

记事本++的早期版本(v5.9.8及更早版本)仅支持标准 POSIX 正则表达式。但是,版本6.0中添加了完整的 PCRE (Perl Compatible Regular Expression)搜索/替换支持。


Notepad ++ 6.0中的新功能和增强功能:




  • 支持PCRE(Perl兼容的正则表达式)。


这意味着如果您使用Notepad ++ v6.0或任何较新版本(例如v6您可以使用PCRE语法,并使用 $ 1 而不是 \1 进行反向引用,但是将不兼容早期版本的Notepad ++(版本6.0之前)。除此之外,它们是相似的。



有关反向引用语法与新语法支持背后原因之间的差异的更多信息,请参阅 替换字符串中的反向引用语法(为什么美元符号?)



可以在Notepad ++中使用有关如何使用正则表达式的有用教程 这里


I have tried to use the Notepad++ Search/Replace with a Regular Expression to replace specific words with shorter versions of those words.


I used the following regex to match every word that ends with er (but not er as a word) - and replace the matching words with the same words minus the ending r, using a backreference:

Find what: ([a-zA-z]+e)r

Replace with: $1

But it doesn't replace the matching words, even though it finds them.

However, if I change the backreference syntax to this:

Replace with: \1

Everything works fine.


Why doesn't the $1 backreference work?

What is the difference between the two forms of the backrefernce - \1 and $1?

解决方案

Notepad++'s earlier versions (v5.9.8 and prior) only supported standard POSIX Regular Expressions. However, full PCRE (Perl Compatible Regular Expression) Search/Replace support was added in version 6.0:

New features and enhancement in Notepad++ 6.0:

  • PCRE (Perl Compatible Regular Expressions) is supported.

This means that if you're using Notepad++ v6.0 or any newer version (e.g v6.1.5), you can use the PCRE syntax, and use $1 instead of \1 for backreference, but it won't be compatible with earlier versions of Notepad++ (prior to version 6.0). Other than that, they're similar.

For more info regarding the differences between the backreference syntax and the reasons behind the new syntax support, see Backreferences syntax in replacement strings (why dollar sign?).

A useful tutorial on how to use regular expressions in Notepad++ can be found here.

这篇关于Notepad ++ Regex反向引用语法在Search / Replace - \1或$ 1中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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