Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do named and unnamed PCRE capturing groups interact?

Tags:

regex

pcre

If the regular expression is e.g. ^(?<object>[\-\w]+)/([\-\w]+)$, will one invoke the second capturing group as $2 or as $1? In other words, are anonymous capturing groups absolutely or relatively numbered?

like image 693
Urhixidur Avatar asked Jan 20 '26 03:01

Urhixidur


1 Answers

Use $2 to refer to the second numbered capturing group. Note I would not call it anonymous, maybe, "unnamed" would suit better here.

See a sample regex demo.

enter image description here

See PCRE docs:

PCRE supports the use of named as well as numbered capturing parentheses. The names are just an additional way of identifying the parentheses, which still acquire numbers.

like image 174
Wiktor Stribiżew Avatar answered Jan 21 '26 18:01

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!