libzypp  17.24.1
Regex.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_REGEX_H
13 #define ZYPP_BASE_REGEX_H
14 
15 #include <iosfwd>
16 #include <string>
17 #include <regex.h>
18 
19 #include <zypp/base/Exception.h>
20 
22 namespace zypp
23 {
27  namespace str
28  {
53 
55 
56  class smatch;
57  class regex;
58 
67  bool regex_match( const char * s, smatch & matches, const regex & regex );
68 
70  inline bool regex_match(const std::string & s, smatch & matches, const regex & regex)
71  { return regex_match( s.c_str(), matches, regex ); }
72 
74  bool regex_match( const char * s, const regex & regex );
75 
77  inline bool regex_match( const std::string & s, const regex & regex )
78  { return regex_match( s.c_str(), regex ); }
79 
86  std::string regex_substitute ( const std::string & s, const regex & regex, const std::string &replacement, bool global = true );
87 
94  class regex
95  {
96  public:
97 
98  enum RegFlags {
99  icase = REG_ICASE,
100  nosubs = REG_NOSUB,
101  match_extended = REG_EXTENDED,
102  newline = REG_NEWLINE
103  };
104 
105  enum MatchFlags {
106  none = 0,
107  not_bol = REG_NOTBOL
108  };
109 
110  regex();
111  regex(const std::string& s,int flags = match_extended);
112  ~regex() throw();
113 
114  regex(const regex & rhs)
115  { assign(rhs.m_str, rhs.m_flags); }
116 
117  regex & operator=(const regex & rhs)
118  { assign(rhs.m_str, rhs.m_flags); return *this; }
119 
123  std::string asString() const
124  { return m_str; }
125 
126  bool matches (const char * s, str::smatch& matches, int flags = none ) const;
127  bool matches ( const char * s ) const;
128 
129  public:
131  regex_t * get()
132  { return & m_preg; }
133 
134  private:
135  void assign(const std::string& s,int flags = match_extended);
136 
137  private:
138  friend class smatch;
139  std::string m_str;
140  int m_flags;
141  regex_t m_preg;
142  bool m_valid;
143  };
144 
146  inline std::ostream & operator<<( std::ostream & str, const regex & obj )
147  { return str << obj.asString(); }
148 
160  class smatch
161  {
162  public:
163  smatch();
164 
165  std::string operator[](unsigned i) const;
166 
167  unsigned size() const;
168 
170  std::string::size_type begin( unsigned i ) const;
171 
173  std::string::size_type end( unsigned i ) const;
174 
176  std::string::size_type size( unsigned i ) const;
177 
178  std::string match_str;
179  std::vector<regmatch_t> pmatch;
180  };
181 
182  } // namespace str
184 } // namespace zypp
186 #endif // ZYPP_BASE_STRING_H
unsigned size() const
Definition: Regex.cc:107
std::string regex_substitute(const std::string &s, const regex &regex, const std::string &replacement, bool global=true)
Replaces the matched regex with the string passed in replacement.
Definition: Regex.cc:121
Regular expression.
Definition: Regex.h:94
regex_t m_preg
Definition: Regex.h:141
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const regex &obj)
Definition: Regex.h:146
std::string::size_type end(unsigned i) const
End index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:101
std::string::size_type begin(unsigned i) const
Begin index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:98
Match newline.
Definition: Regex.h:102
Support for substring addressing of matches is not required.
Definition: Regex.h:100
Exception regex_error
Definition: Regex.h:54
Do not differentiate case.
Definition: Regex.h:99
Do not match begin of line.
Definition: Regex.h:107
bool m_valid
Definition: Regex.h:142
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition: Regex.cc:60
regex & operator=(const regex &rhs)
Definition: Regex.h:117
std::string m_str
Definition: Regex.h:139
SolvableIdType size_type
Definition: PoolMember.h:126
Regular expression match result.
Definition: Regex.h:160
std::string match_str
Definition: Regex.h:178
Base class for Exception.
Definition: Exception.h:145
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition: Regex.h:101
void assign(const std::string &s, int flags=match_extended)
Definition: Regex.cc:29
std::string operator[](unsigned i) const
Definition: Regex.cc:90
std::string asString() const
string representation of the regular expression
Definition: Regex.h:123
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
std::vector< regmatch_t > pmatch
Definition: Regex.h:179
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1