I’m sure this is simple and I’ll probably be embarrassed to have this question in my profile but I can’t seem to get this Regex correct.
I’m tring to extract just the digits from the last group of the following string:
Properties[1].Securitymeasures[14].AdditionalSecurityType
so I want to have a regex that will return 14
The Regex I have come up with is:
\[(\d)+\]
However – the match is returning “[14]” – including the brackets and I do not understand why. I have surrounded the \d with parenthesis which should mean that this is the data I want to capture.
Well, your regex is actually matching
So the fact that it returns the brackets is entirely correct. If you debug the following
You’ll see that the \d is captured as a group (you put it in parens, after all).
What you want to do is use a “Positive Lookbehind” to specify that you are looking for one or more digits after a bracket.
that says
You can bracket that with a Positive Lookahead, but if you are parsing integer indexes, then you can always expect there to be only digits within brackets and can skip the lookahead.