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:
-
@Anton ,
As it says in the Searching > Regular Expressions > Substitutions section of the user manual

\<hi>That is never a valid syntax. I assume you were trying for the “backreference” notation of
\g<hi>or\k<hi>. But as the backreference user manual section says,
, 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)or(?'name'subset)or(?(name)subset)⇒ Named Capture Group:The first two work for me:
Search:(?<hi>world)
Replace:$+{hi}
and
Search:(?'hi'world)
Replace:$+{hi}This fails with
Invalid Regular Expressionstarting at the second(
Search:(?(hi)world)
Replace:$+{hi}The Boost manual section in this only mentions the two forms that work.
-
@mkupper said in How to refer named group in RegExp-replacement?:
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:
\g'name'or\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.
For example,
(?'int'[-+]?\d+)\h*b+\h*(?&int)
matches an integer with leading + or minus, somebcharacters, and then another integer (referenced with(?&int))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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login