Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture group reference + numeral

Tags:

regex

For example, I would like to replace a string by the first capture group, with a 1 appended to it.

I would like to do $11 (interpreted as $1 and 1), but this does not work on every flavour.

What should I do instead?

like image 695
Kenny Lau Avatar asked Feb 10 '26 15:02

Kenny Lau


1 Answers

The info at the Retina link you provided says:

Under the hood, it uses .NET's regex engine, which means that both the .NET flavour and the ECMAScript flavour are available.

So, if you are using the .NET flavor, use ${1}1 to replace with the first backreference and 1, use ${11} or $11 to replace with the 11th backreference.

If you are using an ECMAScript expression, $11 will be treated this way: if there are fewer than 11 capture groups, but there is 1, $1 will be parsed as the backreference to Group 1 followed with a literal 1; if there are 11 capture groups, it will be a backreference to the 11th group.

like image 146
Wiktor Stribiżew Avatar answered Feb 13 '26 08:02

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!