I have the following example of a string with regex which I am trying to match:
Regex:
^\d{3}( [0-9a-fA-F]{2}){3}
String to match:
010 00 00 00
My question is this – the regex matches and captures 1 group – the final 00 at the end of the string. However, I want it to match all three of the 00 groups at the end. Why doesn’t this work? Surely the brackets should mean that they are all matched equally?
I know that I could just type out the three groups separately but this is just a short extract of a much longer string so that would be a pain. I was hoping that this would provide a more elegant solution but it seems my understanding is lacking somewhat!
Thanks!
Because you have a quantifier on a capture group, you’re only seeing the capture from the last iteration. Luckily for you though, .NET (unlike other implementations) provides a mechanism for retrieving captures from all iterations, via the CaptureCollection class. From the linked documentation:
And the example provided from the linked documentation: