GCS  0.2.3
gu_regex.hpp
1 // Copyright (C) 2009 Codership Oy <info@codership.com>
2 
9 #ifndef _gu_regex_hpp_
10 #define _gu_regex_hpp_
11 
12 #include <regex.h>
13 #include <string>
14 #include <vector>
15 
16 #include "gu_throw.hpp"
17 
18 namespace gu
19 {
20  class RegEx
21  {
22  regex_t regex;
23 
24  std::string strerror (int rc) const;
25 
26  public:
27 
31  RegEx (const std::string& expr) : regex()
32  {
33  int rc;
34 
35  if ((rc = regcomp(&regex, expr.c_str(), REG_EXTENDED)) != 0)
36  {
37  gu_throw_fatal << "regcomp(" << expr << "): " << strerror(rc);
38  }
39  }
40 
41  ~RegEx ()
42  {
43  regfree (&regex);
44  }
45 
50  class Match
51  {
52  std::string value;
53  bool set;
54 
55  public:
56 
57  Match() : value(), set(false) {}
58  Match(const std::string& s) : value(s), set(true) {}
59 
60  // throws NotSet
61  const std::string& str() const
62  {
63  if (set) return value;
64 
65  throw NotSet();
66  }
67 
68  bool is_set() const { return set; }
69  };
70 
79  std::vector<Match>
80  match (const std::string& str, size_t num) const;
81  };
82 }
83 
84 #endif /* _gu_regex_hpp_ */
Definition: gu_regex.hpp:20
Definition: gu_regex.hpp:50
RegEx(const std::string &expr)
Definition: gu_regex.hpp:31
std::vector< Match > match(const std::string &str, size_t num) const
Matches given string.
Definition: gu_exception.hpp:16