1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Regex.h
12 #ifndef ZYPP_BASE_REGEX_H
13 #define ZYPP_BASE_REGEX_H
19 #include "zypp/base/Exception.h"
21 //////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////
26 /// \brief String related utilities and \ref ZYPP_STR_REGEX.
29 //////////////////////////////////////////////////////////////////
30 /// \defgroup ZYPP_STR_REGEX Regular expression matching
31 /// \brief Regular expressions using the glibc regex library.
33 /// \see also \ref StrMatcher string matcher also supporting globing, etc.
36 /// str::regex rxexpr( "^(A)?([0-9]*) im" );
39 /// std::string mytext( "Y123 imXXXX" );
40 /// if ( str::regex_match( mytext, what, rxexpr ) )
42 /// MIL << "MATCH '" << what[0] << "'" << endl;
43 /// MIL << " subs: " << what.size()-1 << endl;
44 /// for_( i, 1U, what.size() )
45 /// MIL << " [" << i << "] " << what[i] << endl;
49 /// WAR << "NO MATCH '" << rxexpr << "' in '" << mytext << endl;
52 //////////////////////////////////////////////////////////////////
54 typedef Exception regex_error;
59 //////////////////////////////////////////////////////////////////
60 /// \brief Regular expression matching
62 /// \ingroup ZYPP_STR_REGEX
64 /// Return whether a \ref regex matches a specific string. An optionally
65 /// passed \ref smatch object will contain the match reults.
66 //////////////////////////////////////////////////////////////////
67 bool regex_match( const char * s, smatch & matches, const regex & regex );
69 /** \copydoc regex_match \relates regex \ingroup ZYPP_STR_REGEX */
70 inline bool regex_match(const std::string & s, smatch & matches, const regex & regex)
71 { return regex_match( s.c_str(), matches, regex ); }
73 /** \copydoc regex_match \relates regex \ingroup ZYPP_STR_REGEX */
74 bool regex_match( const char * s, const regex & regex );
76 /** \copydoc regex_match \relates regex \ingroup ZYPP_STR_REGEX */
77 inline bool regex_match( const std::string & s, const regex & regex )
78 { return regex_match( s.c_str(), regex ); }
80 //////////////////////////////////////////////////////////////////
82 /// \brief Regular expression
84 /// \ingroup ZYPP_STR_REGEX
85 //////////////////////////////////////////////////////////////////
91 optimize = 0, ///< \deprecated legacy, obsolete
92 match_extra = 0, ///< \deprecated legacy, obsolete
93 icase = REG_ICASE, ///< Do not differentiate case
94 nosubs = REG_NOSUB, ///< Support for substring addressing of matches is not required
95 match_extended = REG_EXTENDED, ///< Use POSIX Extended Regular Expression syntax when interpreting regex.
96 normal = 1<<16 ///< \deprecated legacy, use match_extended
100 regex(const std::string& s,int flags = match_extended);
103 regex(const regex & rhs)
104 { assign(rhs.m_str, rhs.m_flags); }
106 regex & operator=(const regex & rhs)
107 { assign(rhs.m_str, rhs.m_flags); return *this; }
110 * string representation of the regular expression
112 std::string asString() const
116 /** Expert backdoor. Returns pointer to the compiled regex for direct use in regexec() */
121 void assign(const std::string& s,int flags = match_extended);
125 friend bool regex_match(const char * s, str::smatch& matches, const regex& regex);
126 friend bool regex_match(const char * s, const regex& regex);
133 /** \relates regex Stream output */
134 inline std::ostream & operator<<( std::ostream & str, const regex & obj )
135 { return str << obj.asString(); }
137 //////////////////////////////////////////////////////////////////
139 /// \brief Regular expression match result
141 /// \ingroup ZYPP_STR_REGEX
143 /// Index \c n=0 returns the string object representing the character
144 /// sequence that matched the whole regular expression.
145 /// If \c n is out of range, or if \c n is an unmatched sub-expression,
146 /// then an empty string is returned.
147 //////////////////////////////////////////////////////////////////
153 std::string operator[](unsigned i) const;
155 unsigned size() const;
157 std::string match_str;
158 regmatch_t pmatch[12];
162 //////////////////////////////////////////////////////////////////
164 //////////////////////////////////////////////////////////////////
165 #endif // ZYPP_BASE_STRING_H