I posted this question earlier.
But that wasn't quite the end of it. All the rules that applied there still apply.
So the strings:
"%ABC%"
would yield ABC as a result (capture stuff between percent signs)- as would
"$ABC."
(capture stuff after $, giving up when another dollar or dot appears) "$ABC$XYZ"
would too, and also give XYZ as a result.
To add a bit more to this:
"${ABC}"
should yield ABC too. (ignore curly braces if present - non capture chars perhaps?).- if you have two successive dollar signs, such as
"$$EFG"
, or"$${EFG}"
,
that should not appear in a regex result. (This is where either numbered or named back- references come into play - and the reason I contemplated them as non-capture groups). As I understand it, a group becomes a non-capture group with this syntax(?:)
.
1) Can I say the % or $ is a non-capture group and reference that by number? Or do only capture groups get allocated numbers?
2) What is the order of the numbering, if you have ((A) (B) (C))
. Is the outer group 1, A 2, B 3 C 4?
I have been look at named groups. Saw the syntax mentioned here
(?<name>capturing text)
to define a named group "name"
\k<name>
to backreference a named group "name"
3) Not sure if a non-capture group can be named in Java? Can someone elucidate?
- More info here on non capture groups.
- More info here on lookbehinds
- Similar answer to a question here, but didn't quite get me what I wanted. Not sure if there is a back-reference issue in Java.
- Similar question here. But could not get my head around the working version to apply to this.
I have used the exact same Java I had in my original question, except for:
String search = "/bla/$V_N.$$XYZ.bla";
String pattern = "(?:(?<oc>[%$]))(?!(\\k<oc>))([^%.$]*)+";
This should only result in V_N.
I am really struggling with this one, and wondered if someone can help me work out how to solve this. Thanks.