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, allowing it to be
12 used in constructs like \code if(RegMatch match=regex.match("foo")) \endcode.
14 A RegMatch representing a successful match has one or more groups, indicating
15 matching parts of the string. The first group (with index 0) indicates the
16 part matched by the whol regex. Further groups, if present, indicate parts
17 matched by subregexes. These are ordered from left to right, by the opening
18 parenthesis of the subregex.
24 A single subregex of the match.
28 bool match; //< Whether or not this group matched
29 unsigned begin; //< First offset of the match
30 unsigned end; //< One-past-last offset
31 unsigned length; //< Length of the match (end-begin)
32 std::string str; //< The part of the string that matched
34 Group(): match(false) { }
35 operator bool() const { return match; }
37 typedef std::vector<Group> GroupArray;
40 Constructs a RegMatch representig a non-match. Used by Regex.
45 Constructs a new RegMatch from a string and groups. The length and str members
46 of each group are computed and need not be set. Used by Regex.
48 RegMatch(const std::string &, const std::vector<Group> &);
51 Returns a reference to a single group in the match. An exception is thrown
52 if the requested group does not exist.
54 const Group &group(unsigned) const;
57 Returns true if the RegMatch object represents a non-match.
59 bool empty() const { return groups.empty(); }
62 Returns the number of groups in this match.
64 unsigned size() const { return groups.size(); }
67 Returns the begin offset of the whole match.
69 unsigned begin() const { return groups.empty()?0:groups[0].begin; }
72 Returns the end offset of the whole match.
74 unsigned end() const { return groups.empty()?0:groups[0].end; }
77 Shortcut for the group() function.
79 const Group &operator[](unsigned i) const { return group(i); }
81 operator bool() const { return !empty(); }
83 std::vector<Group> groups;