3 This file is part of libmspstrings
4 Copyright © 2007 Mikko Rasa
5 Distributed under the LGPL
7 #ifndef MSP_STRINGS_REGMATCH_H_
8 #define MSP_STRINGS_REGMATCH_H_
16 This class stores the result of a Regex being matched against a string. If the
17 match was successful, the RegMatch object evaluates to true, allowing it to be
18 used in constructs like \code if(RegMatch match=regex.match("foo")) \endcode.
20 A RegMatch representing a successful match has one or more groups, indicating
21 matching parts of the string. The first group (with index 0) indicates the
22 part matched by the whol regex. Further groups, if present, indicate parts
23 matched by subregexes. These are ordered from left to right, by the opening
24 parenthesis of the subregex.
30 A single subregex of the match.
34 bool match; //< Whether or not this group matched
35 unsigned begin; //< First offset of the match
36 unsigned end; //< One-past-last offset
37 unsigned length; //< Length of the match (end-begin)
38 std::string str; //< The part of the string that matched
40 Group(): match(false) { }
41 operator bool() const { return match; }
43 typedef std::vector<Group> GroupArray;
46 Constructs a RegMatch representig a non-match. Used by Regex.
51 Constructs a new RegMatch from a string and groups. The length and str members
52 of each group are computed and need not be set. Used by Regex.
54 RegMatch(const std::string &, const std::vector<Group> &);
57 Returns a reference to a single group in the match. An exception is thrown
58 if the requested group does not exist.
60 const Group &group(unsigned) const;
63 Returns true if the RegMatch object represents a non-match.
65 bool empty() const { return groups.empty(); }
68 Returns the number of groups in this match.
70 unsigned size() const { return groups.size(); }
73 Returns the begin offset of the whole match.
75 unsigned begin() const { return groups.empty()?0:groups[0].begin; }
78 Returns the end offset of the whole match.
80 unsigned end() const { return groups.empty()?0:groups[0].end; }
83 Shortcut for the group() function.
85 const Group &operator[](unsigned i) const { return group(i); }
87 operator bool() const { return !empty(); }
89 std::vector<Group> groups;