简体   繁体   English

如何在Notepad ++的“正则表达式查找器”对话框中替换周围的文本?

[英]How to Replace Text Surrounding Text in Notepad++'s Regular Expression Finder Dialog?

This question is the opposite from other regex notepad++ questions. 此问题与其他正则表达式notepad ++问题相反。 Instead of changing text between text, I need to replace text that is surrounding, like that: 除了替换文本之间的文本,我还需要替换周围的文本,如下所示:

from

$_REQUEST[' action '] $ _REQUEST [' 操作 ']

to

getReq(' action ') getReq(' action ')

So: 所以:

I wish to replace $_REQUEST( for getReq( and at same time replace ] for ) . 我希望将$_REQUEST(替换为getReq( ,同时将]替换为)

How can I achieve that in Notepad++ ? 如何在Notepad ++中实现呢? There are more than a 1000 hits and I want to replace it all, not just the ones with action index, but many more! 有超过1000个匹配,我想替换所有匹配,不仅仅是具有动作索引的匹配,还有更多!

You still want to match, you just want match with capture (Notepad supports this, make sure Regex is checked in search and replace. The ( ) is the capture group, the order of the first ( is the capture group number. (?: ) can be used to make a group non-capture) 您仍然想要匹配,只想与捕获匹配(记事本支持此操作,请确保已在搜索和替换中选中了正则表达式。()是捕获组,第一个顺序(是捕获组号。(?: )可用于使群组不被捕获)

Match on \\$_REQUEST\\['([^']*)'\\] 匹配\\$_REQUEST\\['([^']*)'\\]

Replace using capture with getReq\\('$1'\\) 将捕获替换为getReq\\('$1'\\)

EDIT: In Notepad, you have to escape () for some reason in the replace part 编辑:在记事本中,由于某种原因,您必须在替换部分中转义()

You need to use a capturing group in your regex. 您需要在正则表达式中使用捕获组 In most regex engines, capturing groups are indicated with parentheses, possibly escaped with backslashes in front: 在大多数正则表达式引擎中,捕获组用括号表示,可能在前面用反斜杠转义:

foo(capturing_group)bar

foo\(capturing group\)bar

Notepad++ uses PCRE, I think, so it should be bare parens (the first example, above). 我认为Notepad ++使用PCRE,因此它应该是空白的(上面的第一个示例)。

What you have is a larger pattern: 您拥有的是更大的模式:

$_REQUEST['some variable text goes here']

You want to replace that with 您想用替换

getReq('some variable text goes here')

The capturing group will "capture" (or "save") the variable text, and a backreference to the group will "insert" the text in your replacement: 捕获组将“捕获”(或“保存”)变量文本,对该组的向后引用将在替换中“插入”文本:

$_REQUEST['([^']*)']   

getReq('\1')

The search would be for the outside text, $_REQUEST[' and '] , plus a capturing group ( ... ) containing [^']* any number of characters that are not single quotes. 搜索将查找外部文本$_REQUEST[''] ,再加上一个捕获组( ... )其中包含[^']*任意数量的非单引号字符。

The replacement would be the outside replacement text, getReq(' and ') , plus a backreference to the first (and only!) capturing group in the original match. 替换将是外部替换文本getReq('') ,再加上对原始匹配中第一个(也是唯一的!)捕获组的反向引用。 The \\1 is replaced by everything matched inside the first set of parens. \\1被替换为第一组括号内的所有内容。

FYI: Groups are generally numbered by counting opening parentheses. 仅供参考:分组通常通过计算左括号来编号。 So a nested group like this: ( ( ) ) ( ( ) ) would be numbered (1 (2 ) ) (3 (4 ) ) . 因此,这样的嵌套组: ( ( ) ) ( ( ) )将被编号为(1 (2 ) ) (3 (4 ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM