Contents Up Previous Next

wxRegEx

wxRegEx represents a regular expression. The regular expressions syntax supported is the POSIX one. Both basic and extended regular expressions are supported but, unlike POSIX C API, the extended ones are used by default.

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. In the latter case you need to abide by the terms of its copyright:

Copyright 1992, 1993, 1994, 1997 Henry Spencer.  All rights reserved.
This software is not subject to any license of the American Telephone
and Telegraph Company or of the Regents of the University of California.

Permission is granted to anyone to use this software for any purpose on
any computer system, and to alter it and redistribute it, subject
to the following restrictions:

1. The author is not responsible for the consequences of use of this
   software, no matter how awful, even if they arise from flaws in it.

2. The origin of this software must not be misrepresented, either by
   explicit claim or by omission.  Since few users ever read sources,
   credits must appear in the documentation.

3. Altered versions must be plainly marked as such, and must not be
   misrepresented as being the original software.  Since few users
   ever read sources, credits must appear in the documentation.

4. This notice may not be removed or altered.
Derived from

No base class

Data structures

Flags for regex compilation to be used with Compile():

enum
{
    // use extended regex syntax (default)
    wxRE_EXTENDED = 0,

    // 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 = "([^@]+)@([[:alnum:].-_].)+([[:alnum:]]+)";
if ( reEmail.Matches(text) )
{
    wxString text = reEmail.GetMatch(email);
    wxString username = reEmail.GetMatch(email, 1);
    if ( reEmail.GetMatch(email, 3) == "com" ) // .com TLD?
    {
        ...
    }
}

// or we could do this to hide the email address
size_t count = reEmail.ReplaceAll(text, "HIDDEN@\\2\\3");
printf("text now contains %u hidden addresses", count);
Members

wxRegEx::wxRegEx
wxRegEx::~wxRegEx
wxRegEx::Compile
wxRegEx::IsValid
wxRegEx::GetMatch
wxRegEx::Matches
wxRegEx::Replace
wxRegEx::ReplaceAll
wxRegEx::ReplaceFirst


wxRegEx::wxRegEx

wxRegEx()

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::Compile

bool 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::IsValid

bool IsValid() const

Return TRUE if this is a valid compiled regular expression, FALSE otherwise.


wxRegEx::GetMatch

bool 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 occured.

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::Matches

bool Matches(const wxChar* 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.

May only be called after successful call to Compile().


wxRegEx::Replace

int 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::ReplaceAll

int ReplaceAll(wxString* text, const wxString& replacement) const

Replace all occurrences: this is actually a synonym for Replace().

See also

ReplaceFirst


wxRegEx::ReplaceFirst

int ReplaceFirst(wxString* text, const wxString& replacement) const

Replace the first occurrence.

See also

Replace