wxregexwxregex represents a regular expression. this class provides support for regular expressions matching and also replacement. it is built on top of either the system library (if it has support for posix regular expressions - which is the case of the most modern unices) or uses the built in henry spencer's library. henry spencer would appreciate being given credit in the documentation of software which uses his library, but that is not a requirement. regular expressions, as defined by posix, come in two flavours: extended and basic. the builtin library also adds a third flavour of expression advanced, which is not available when using the system library. unicode is fully supported only when using the builtin library. when using the system library in unicode mode, the expressions and data are translated to the default 8-bit encoding before being passed to the library. on platforms where a system library is available, the default is to use the builtin library for unicode builds, and the system library otherwise. it is possible to use the other if preferred by selecting it when building the wxwidgets. derived from no base class data structures flags for regex compilation to be used with compile():
enum { // use extended regex syntax wxre_extended = 0, // use advanced re syntax (built-in regex only) #ifdef wxhas_regex_advanced wxre_advanced = 1, #endif // use basic re syntax wxre_basic = 2, // ignore case in match wxre_icase = 4, // only check match, don't set back references wxre_nosub = 8, // if not set, treat '\n' as an ordinary character, otherwise it is // special: it is not matched by '.' and '^' and '$' always match // after/before it regardless of the setting of wxre_not[be]ol wxre_newline = 16, // default flags wxre_default = wxre_extended }flags for regex matching to be used with matches(). these flags are mainly useful when doing several matches in a long string to prevent erroneous matches for '' and '$':
enum { // '^' doesn't match at the start of line wxre_notbol = 32, // '$' doesn't match at the end of line wxre_noteol = 64 }examples a bad example of processing some text containing email addresses (the example is bad because the real email addresses can have more complicated form than user@host.net):
wxstring text; ... wxregex reemail = wxt("([^@]+)@([[:alnum:].-_].)+([[:alnum:]]+)"); if ( reemail.matches(text) ) { wxstring text = reemail.getmatch(email); wxstring username = reemail.getmatch(email, 1); if ( reemail.getmatch(email, 3) == wxt("com") ) // .com tld? { ... } } // or we could do this to hide the email address size_t count = reemail.replaceall(text, wxt("hidden@\\2\\3")); printf("text now contains %u hidden addresses", count);include files <wx/regex.h> members
wxregex::wxregex
wxregex::wxregexwxregex() default ctor: use compile() later. wxregex(const wxstring& expr, int flags = wxre_default) create and compile the regular expression, use isvalid to test for compilation errors.
wxregex::~wxregex~wxregex() dtor not virtual, don't derive from this class
wxregex::compilebool compile(const wxstring& pattern, int flags = wxre_default) compile the string into regular expression, return true if ok or false if string has a syntax error.
wxregex::isvalidbool isvalid() const return true if this is a valid compiled regular expression, false otherwise.
wxregex::getmatchbool getmatch(size_t* start, size_t* len, size_t index = 0) const get the start index and the length of the match of the expression (if index is 0) or a bracketed subexpression (index different from 0). may only be called after successful call to matches() and only if wxre_nosub was not used in compile(). returns false if no match or if an error occurred. wxstring getmatch(const wxstring& text, size_t index = 0) const returns the part of string corresponding to the match where index is interpreted as above. empty string is returned if match failed may only be called after successful call to matches() and only if wxre_nosub was not used in compile().
wxregex::getmatchcountsize_t getmatchcount() const returns the size of the array of matches, i.e. the number of bracketed subexpressions plus one for the expression itself, or 0 on error. may only be called after successful call to compile(). and only if wxre_nosub was not used.
wxregex::matchesbool matches(const wxchar* text, int flags = 0) const bool matches(const wxchar* text, int flags, size_t len) const bool matches(const wxstring& text, int flags = 0) const matches the precompiled regular expression against the string text, returns true if matches and false otherwise. flags may be combination of wxre_notbol and wxre_noteol. some regex libraries assume that the text given is null terminated, while others require the length be given as a separate parameter. therefore for maximum portability assume that text cannot contain embedded nulls. when the matches(const wxchar *text, int flags = 0) form is used, a wxstrlen() will be done internally if the regex library requires the length. when using matches() in a loop the matches(text, flags, len) form can be used instead, making it possible to avoid a wxstrlen() inside the loop. may only be called after successful call to compile().
wxregex::replaceint replace(wxstring* text, const wxstring& replacement, size_t maxmatches = 0) const replaces the current regular expression in the string pointed to by text, with the text in replacement and return number of matches replaced (maybe 0 if none found) or -1 on error. the replacement text may contain back references \number which will be replaced with the value of the corresponding subexpression in the pattern match. \0 corresponds to the entire match and & is a synonym for it. backslash may be used to quote itself or & character. maxmatches may be used to limit the number of replacements made, setting it to 1, for example, will only replace first occurrence (if any) of the pattern in the text while default value of 0 means replace all.
wxregex::replaceallint replaceall(wxstring* text, const wxstring& replacement) const replace all occurrences: this is actually a synonym for replace(). see also
wxregex::replacefirstint replacefirst(wxstring* text, const wxstring& replacement) const replace the first occurrence. see also
|