I am making a small applicaiton using .NET Regex types. And the “Capture, Group and Match” types totally confused me. I have never seen such an ugly solution. Could someone explain their usage for me? Many thanks.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s a simpler example than the one in the document @Dav cited:
result:
full match: foo%123%456%789group #1: foogroup #2: 789group #2 captures: 123, 456, 789The
full matchandgroup #1results are straightforward, but the others require some explanation. Group #2, as you can see, is inside a non-capturing group that’s controlled by a+quantifier. It matches three times, but if you request itsValue, you only get what it matched the third time around–the final capture. Similarly, if you use the$2placeholder in a replacement string, the final capture is what gets inserted in its place.In most regex flavors, that’s all you can get; each intermediate capture is overwritten by the next and lost; .NET is almost unique in preserving all of the captures and making them available after the match is performed. You can access them directly as I did here, or iterate through the
CaptureCollectionas you would aMatchCollection. There’s no equivalent for the$1-style replacement-string placeholders, though.So the reason the API design is so ugly (as you put it) is twofold: first it was adapted from Perl’s integral regex support to .NET’s object-oriented framework; then the
CaptureCollectionstructure was grafted onto it. Perl 6 offers a much cleaner solution, but the authors accomplished that by rewriting Perl practically from scratch and throwing backward compatibility out the window.