2

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)*?"

IanWatson
  • 1,649
  • 2
  • 27
  • 49
  • 1
    Don't parse XML with RegEx! See http://stackoverflow.com/questions/8577060/why-is-it-such-a-bad-idea-to-parse-xml-with-regex – AlexR Aug 11 '16 at 16:11

1 Answers1

6

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.

Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39
  • Might also be worth mentioning named capturing groups to avoid getting mixed up on which group is which and being able to explicitly reference a certain group. – R Nar Aug 11 '16 at 16:06
  • I don't quite understand what you mean here. Can you provide a example? – IanWatson Aug 11 '16 at 16:11
  • 1
    [you can name your capturing groups](http://www.rexegg.com/regex-capture.html#namedgroups) and back-reference them with those names. Although not directly answering the question put forward, I thought it was worth mentioning to avoid any confusion caused by the nested brackets. You can then do something like `(?ab(?ba))` to have `${first} == abba` and `${second} == ba` – R Nar Aug 11 '16 at 16:20