通过正则表达式在Notepad ++中进行替换时如何使用条件 [英] How to use conditionals when replacing in Notepad++ via regex

查看:129
本文介绍了通过正则表达式在Notepad ++中进行替换时如何使用条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下正则表达式:

Consider the following regex:

([a-zA-Z])([a-zA-Z]?)/( [a-zA-Z])([a-zA-Z]?)

如果文本为: a / b
捕获组将为:

If the text is: a/b the capturing groups will be:

/1  'a'
/2  ''
/3  'b'
/4  ''

如果文本是: aa / b
,则捕获组将是:

And if the text is: aa/b the capturing groups will be:

/1  'a'
/2  'a'
/3  'b'
/4  ''

假设,我想在Notepad ++中查找并替换此字符串,使得如果 / 2 / 4 为空(如上述第一种情况),我将 c 作为前缀。

Suppose, I want to find and replace this string in Notepad++ such that if /2 or /4 are empty (as in the first case above), I prepend c.

因此,文本 a / b 变为 ca /​​ cb
并且文本 aa / b 变为 aa / cb

So, the text a/b becomes ca/cb. And the text aa/b becomes aa/cb

我使用以下正则表达式进行替换:

I use the following regex for replacing:

(?(2)\1\2|0\1)/(?(4)\3\4|0\3)

但是在这种情况下,Notepad ++实际上将当作是条件标识符,而不是作为条件标识符。知道我在做什么错吗?

But Notepad++ is treating ? literally in this case, and not as a conditional identifier. Any idea what am I doing wrong?

推荐答案

条件替换的语法是

(?{GROUP_MATCHED?}REPLACEMENT_IF_YES:REPLACEMENT_IF_NO}

{} 对于在处理高于9的组并使用命名捕获时必须避免歧义

The { and } are necessary to avoid ambiguity when you deal with groups higher than 9 and with named capture groups.

由于Notepad ++使用 Boost扩展格式字符串语法 ,请参见< a href = http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html#boost_regex.format.boost_format_syntax.conditionals rel = noreferrer>加强文档:

Since Notepad++ uses Boost-Extended Format String Syntax, see this Boost documentation:


字符开始一个条件表达式,一般形式为:

The character ? begins a conditional expression, the general form is:

?Ntrue-expression:false-expression

其中 N 是十进制数字。

如果子表达式 N 匹配,然后评估 true-表达式并发送到输出,否则 false-expression 进行评估并发送到输出。

If sub-expression N was matched, then true-expression is evaluated and sent to output, otherwise false-expression is evaluated and sent to output.

为了防止歧义,通常需要在条件表达式中加上括号。

You will normally need to surround a conditional-expression with parenthesis in order to prevent ambiguities.

例如,如果子表达式<,则格式字符串(?1foo:bar)将用 foo 替换找到的每个匹配项。 code> $ 1 被匹配,否则与 bar 匹配。

For example, the format string (?1foo:bar) will replace each match found with foo if the sub-expression $1 was matched, and with bar otherwise.

索引大于9的子表达式,或用于访问命名子表达式的子句,请使用:

For sub-expressions with an index greater than 9, or for access to named sub-expressions use:

?{INDEX} true-expression:false -表达式

?{NAME} true-expression:false-expression

因此,请使用([a- zA-Z])([a-zA-Z])?/([a-zA-Z])([a-zA-Z])?并替换为(?{2} $ 1 $ 2:c $ 1)/(?{4} $ 3 $ 4:c $ 3)

So, use ([a-zA-Z])([a-zA-Z])?/([a-zA-Z])([a-zA-Z])? and replace with (?{2}$1$2:c$1)/(?{4}$3$4:c$3).

第二个问题是您将量词放置在捕获组内,使该组内的模式可选,但不是整个组。这使得小组始终参与比赛,而条件始终是真(总是匹配)。 应该量化组

The second problem is that you placed the ? quantifier inside the capturing group, making the pattern inside the group optional, but not the whole group. That made the group always "participating in the match", and the condition would be always "true" (always matched). ? should quantify the group.

这篇关于通过正则表达式在Notepad ++中进行替换时如何使用条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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