Notepad ++ 搜索/替换中的正则表达式反向引用语法 - 1 或 $1 [英] Notepad++ Regex Backreference syntax in Search/Replace - 1 or $1

查看:18
本文介绍了Notepad ++ 搜索/替换中的正则表达式反向引用语法 - 1 或 $1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Notepad++ Search/Replace with a Regular Expression 用这些词的较短版本替换特定词.

<小时>

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

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

替换为: $1

但它不会替换匹配的单词,即使它找到了它们.

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

替换为: 1

一切正常.

<小时>

为什么 $1 反向引用不起作用?

反向引用的两种形式 - 1$1 有什么区别?

解决方案

Notepad++ 的早期版本(v5.9.8 及更早版本)仅支持标准 POSIX 正则表达式.但是,在 6.0 版中添加了完整的 PCRE(Perl 兼容正则表达式)搜索/替换支持:

<块引用>

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

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

这意味着如果您使用 Notepad++ v6.0 或任何更新版本(例如 v6.1.5),您可以使用 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 ++ 搜索/替换中的正则表达式反向引用语法 - 1 或 $1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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