Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex nested backreference

Tags:

regex

Is it possible to get a nested back reference to the below regex:

<field(.*)name="(.*)EndTime"(.*)\n((.*)\n)*?<property name="fieldClass" value="java.lang.String"/>\n</field>

Ie the "((.*)\n)*?"

like image 642
IanWatson Avatar asked Apr 05 '26 18:04

IanWatson


1 Answers

Yes, this is quite possible. Just be certain to watch for which numbered group you're using. Capturing groups (and thus backreferences) are numbered according to which opening bracket comes first - so, in this case, the outer brackets would yield \1, and the inner ones would yield \2.

For example, the pattern ((.+)ab)\1\2 will match the string 1234ab1234ab1234. The first capture group will match the 4 numbers plus the ab, while the second (inner) capture group will only match the numbers. Then we repeat each one, yielding the full match.

like image 80
Sebastian Lenartowicz Avatar answered Apr 08 '26 10:04

Sebastian Lenartowicz



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!