1 #ifndef MSP_STRINGS_REGMATCH_H_
2 #define MSP_STRINGS_REGMATCH_H_
10 This class stores the result of a Regex being matched against a string. If the
11 match was successful, the RegMatch object evaluates to true.
13 A RegMatch representing a successful match has one or more groups, indicating
14 matching parts of the string. The first group (with index 0) indicates the
15 part matched by the whole regex. Further groups, if present, indicate parts
16 matched by subregexes. These are ordered from left to right, by the opening
17 parenthesis of the subregex.
23 A single subregex of the match.
27 bool match; //< Whether or not this group matched
28 unsigned begin; //< First offset of the match
29 unsigned end; //< One-past-last offset
30 unsigned length; //< Length of the match (end-begin)
31 std::string str; //< The part of the string that matched
33 Group(): match(false) { }
34 operator bool() const { return match; }
37 typedef std::vector<Group> GroupArray;
43 /** Constructs a RegMatch representing a non-match. */
46 /** Constructs a new RegMatch from a string and groups. The length and str
47 members of each group are computed and need not be set. Intended to be used
48 by the Regex class. */
49 RegMatch(const std::string &, const std::vector<Group> &);
51 /** Returns a reference to a single group in the match. */
52 const Group &group(unsigned) const;
54 /** Returns true if the RegMatch object represents a non-match. */
55 bool empty() const { return groups.empty(); }
57 /** Returns the number of groups in this match. */
58 unsigned size() const { return groups.size(); }
60 /** Returns the begin offset of the whole match. */
61 unsigned begin() const { return groups.empty() ? 0 : groups[0].begin; }
63 /** Returns the end offset of the whole match. */
64 unsigned end() const { return groups.empty() ? 0 : groups[0].end; }
66 /** Shorthand for the group() function. */
67 const Group &operator[](unsigned i) const { return group(i); }
69 operator bool() const { return !empty(); }