How to refer named group in RegExp-replacement?
I want to use named group in RegExp to replace it with some text.
I can’t understand how to refer this group in “replace”-textbox.
I tried this way but failed:
As it says in the Searching > Regular Expressions > Substitutions section of the user manual
so even if you had used the right syntax for a backreference, it would not work in a replacement.
@PeterJones I was curious about this and so experimented: The manuals’s Capture Groups and Backreferences section documents three ways of defining the names:
(?(name)subset)⇒ Named Capture Group:
The first two work for me:
This fails with
Invalid Regular Expressionstarting at the second
The Boost manual section in this only mentions the two forms that work.
only mentions the two forms that work.
You are right. And I confirmed that’s been the case in Boost for the older versions used by N++ throughout my tenure on the User Manual… so apparently the section on named capture groups has always had that extra third group that shouldn’t’ve been there.
Interestingly, just because the Boost docs doesn’t list it doesn’t mean it’s not invisibly supported by that library:
\k'name'as a backreference syntax aren’t mentioned, but it does work. But in this case,
(?(name)...)is neither documented nor supported.
This is being fixed in PR#573
While we’re here, I’d like to talk about a related useful concept, namely how to reference a named pattern (but not the value captured earlier), which is extremely useful if you have some annoyingly long pattern that you need to match multiple times in the same regex.
matches an integer with leading + or minus, some
bcharacters, and then another integer (referenced with
I won’t expound too much on this topic because it’s also in the documentation, but I’ve definitely written many over-long regexes that could have been hugely simplified by this method.