XML technologies / Regular expressions / .NET / Advanced regular expressions in .NET / Capture objects
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
One component of .NET’s object model has so far only been mentioned in passing: Capture objects. Depending on your perspective, they open a new dimension in match results, or Capture objects merely confuse and unnecessarily bloat the regex package.
A Capture object resembles a Group object in representing text matched by capturing parentheses (to capture). Like a Group object, it provides Value (the matched text), Length (the text’s length), and Index (the text’s offset from the start of the subject string).
The crucial difference is that Capture objects belong to Group objects. Each Group object contains a collection of Capture objects representing not only the final match but also text pieces matched along the way to it.
For example, apply the regex ˹^(..)+˼ to the string ›abcdefghijk‹:
Dim M as Match = Regex.Match("abcdefghijk", "^(..)+")The parenthesized expression matches four times, recognizing most of the string: ›abcdefghijk‹. Because plus is outside the parentheses, each iteration overwrites the captured text with the next two characters, leaving M.Groups(1).Value with only ›ij‹. However, M.Groups(1) also contains several Captures representing the letter pairs matched along the way by ˹(..)˼, namely ›ab‹, ›cd‹, ›ef‹, ›gh‹, and ›ij‹:
M.Groups(1).Captures(0).Value ist ›ab‹ M.Groups(1).Captures(1).Value ist ›cd‹ M.Groups(1).Captures(2).Value ist ›ef‹ M.Groups(1).Captures(3).Value ist ›gh‹ M.Groups(1).Captures(4).Value ist ›ij‹ M.Groups(1).Captures.Count ist 5.
Notice that the last Captures value equals the whole match’s value, M.Groups(1).Value. In fact, the Value property of a Group object is merely shorthand for the last Captures value; M.Groups(1).Value is the same as:
M.Groups(1).Captures( M.Groups(1).Captures.Count - 1 ).Value
A few additional points about Captures objects:
M.Groups(1).Capturesis aCaptureCollection, with the usual collection propertiesItemsandCount. Often, however, theItemsproperty is omitted and theCapturesobjects indexed directly:M.Groups(1).Captures(3)(in C#, use square brackets:M.Groups[1].Captures[3]).Capturesobjects have noSuccessmethod. SinceCapturesobjects can exist only if the parentheses matched at least once, test theSuccessmethod of the enclosingGroupobject instead.So far, the
Capturesobjects shown belonged to aGroupobject. Though less important there,Matchobjects also have aCapturesproperty.M.Capturesdirectly accesses theCapturesobject for group zero; in other words,M.Capturesis the same asM.Groups(0).Captures. But group zero represents the entire match, which cannot have intermediate matches. The zerothCaptureCollectiontherefore always contains one element. Since thisCapturesobject has exactly the same data as the zerothGroupobject, neitherM.CapturesnorM.Groups(0).Capturesis particularly useful.
The Capture objects in .NET are an interesting innovation, confusing mainly because they stretch the object model. After struggling through the documentation and finally understanding what they do, I remain uncertain. On one hand, I would like to explore this innovation, but no applications come to mind for which Captures objects seem ideally suited. Perhaps I simply lack the years of experience I have with other regex features.
On the other hand, these Capture groups add work during matching and result encapsulation. That work should be done only when the information is needed. Most cases probably require no Capture group, but the current implementation always creates Group and Capture objects, with their associated GroupCollection and CaptureCollection objects, whenever the Match object is created. The Capture objects therefore always exist. Use them when you find a suitable purpose.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top