libzypp 17.25.7
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
22namespace 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,
104 };
105
107 none = 0,
108 not_bol = REG_NOTBOL
109 };
110
111 regex();
112 regex( const std::string & s, int flags = rxdefault );
113 regex( const char* s, int flags = rxdefault ) : regex( std::string(s?s:""), flags ) {}
114 ~regex();
115
116 regex( const regex & rhs )
117 { assign( rhs.m_str, rhs.m_flags ); }
118
119 regex & operator=( const regex & rhs )
120 { assign( rhs.m_str, rhs.m_flags ); return *this; }
121
125 std::string asString() const
126 { return m_str; }
127
128 bool matches( const char * s, str::smatch & matches, int flags = none ) const;
129 bool matches( const char * s ) const;
130
131 public:
133 regex_t * get()
134 { return & m_preg; }
135
136 private:
137 void assign( const std::string & s, int flags );
138
139 private:
140 friend class smatch;
141 std::string m_str;
143 regex_t m_preg;
144 bool m_valid = false;
145 };
146
148 inline std::ostream & operator<<( std::ostream & str, const regex & obj )
149 { return str << obj.asString(); }
150
162 class smatch
163 {
164 public:
165 smatch();
166
167 std::string operator[](unsigned i) const;
168
169 unsigned size() const;
170
172 std::string::size_type begin( unsigned i ) const;
173
175 std::string::size_type end( unsigned i ) const;
176
178 std::string::size_type size( unsigned i ) const;
179
180 std::string match_str;
181 std::vector<regmatch_t> pmatch;
182 };
183
184 } // namespace str
186} // namespace zypp
188#endif // ZYPP_BASE_STRING_H
Base class for Exception.
Definition: Exception.h:146
Regular expression.
Definition: Regex.h:95
regex_t * get()
Expert backdoor.
Definition: Regex.h:133
std::ostream & operator<<(std::ostream &str, const regex &obj)
Stream output.
Definition: Regex.h:148
regex(const char *s, int flags=rxdefault)
Definition: Regex.h:113
regex_t m_preg
Definition: Regex.h:143
std::string m_str
Definition: Regex.h:141
@ match_extended
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition: Regex.h:101
@ icase
Do not differentiate case.
Definition: Regex.h:99
@ newline
Match newline.
Definition: Regex.h:102
@ rxdefault
These are enforced even if you don't pass them as flag argument.
Definition: Regex.h:103
@ nosubs
Support for substring addressing of matches is not required.
Definition: Regex.h:100
std::string asString() const
string representation of the regular expression
Definition: Regex.h:125
bool m_valid
Definition: Regex.h:144
void assign(const std::string &s, int flags)
Definition: Regex.cc:36
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition: Regex.cc:57
@ not_bol
Do not match begin of line.
Definition: Regex.h:108
regex(const regex &rhs)
Definition: Regex.h:116
regex & operator=(const regex &rhs)
Definition: Regex.h:119
Regular expression match result.
Definition: Regex.h:163
unsigned size() const
Definition: Regex.cc:106
std::string::size_type end(unsigned i) const
End index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:100
std::string::size_type begin(unsigned i) const
Begin index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:97
std::string operator[](unsigned i) const
Definition: Regex.cc:89
std::string match_str
Definition: Regex.h:180
std::vector< regmatch_t > pmatch
Definition: Regex.h:181
bool regex_match(const char *s, smatch &matches, const regex &regex)
Regular expression matching.
Definition: Arch.h:348
String related utilities and Regular expression matching.
SolvableIdType size_type
Definition: PoolMember.h:126
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:120
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
\relates regex \ingroup ZYPP_STR_REGEX \relates regex \ingroup ZYPP_STR_REGEX
Definition: Regex.h:70
Exception regex_error
Definition: Regex.h:54
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2