考虑以下正则表达式:
([a-zA-Z])([a-zA-Z]?)/([a-zA-Z])([a-zA-Z]?)
如果文字是:a/b
捕获组将是:
/1 'a'
/2 ''
/3 'b'
/4 ''
如果文字是:aa/b
捕获组将是:
/1 'a'
/2 'a'
/3 'b'
/4 ''
假设,我想在Notepad ++中找到并替换此字符串,这样如果/2
或/4
为空(如上面的第一种情况),我会添加c
。
因此,文字a/b
变为ca/cb
。
文字aa/b
变为aa/cb
我使用以下正则表达式替换:
(?(2)\1\2|0\1)/(?(4)\3\4|0\3)
但Notepad ++在这种情况下字面上处理?
,而不是条件标识符。知道我做错了什么吗?
答案 0 :(得分:11)
The syntax in the conditional replacement is
(?{GROUP_MATCHED?}REPLACEMENT_IF_YES:REPLACEMENT_IF_NO}
The {
and }
are necessary to avoid ambiguity when you deal with groups higher than 9 and with named capture groups.
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
where
N
is decimal digit.If sub-expression
N
was matched, thentrue-expression
is evaluated and sent to output, otherwisefalse-expression
is evaluated and sent to output.You will normally need to surround a conditional-expression with parenthesis in order to prevent ambiguities.
For example, the format string
(?1foo:bar)
will replace each match found withfoo
if the sub-expression$1
was matched, and withbar
otherwise.For sub-expressions with an index greater than 9, or for access to named sub-expressions use:
?{INDEX}true-expression:false-expression
or
?{NAME}true-expression:false-expression
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.