How to refer named group in RegExp-replacement?

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, some b characters, 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.

Read more here: Source link