2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: regex.h
|
2008-03-10 11:24:38 -04:00
|
|
|
// Purpose: interface of wxRegEx
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-05-04 05:04:38 -04:00
|
|
|
/**
|
2008-06-20 22:38:44 -04:00
|
|
|
@anchor wxRE_FLAGS
|
|
|
|
|
2008-06-21 01:53:53 -04:00
|
|
|
Flags for regex compilation to be used with wxRegEx::Compile().
|
2008-05-04 05:04:38 -04:00
|
|
|
*/
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/** Use extended regex syntax. */
|
|
|
|
wxRE_EXTENDED = 0,
|
|
|
|
|
|
|
|
/** Use advanced RE syntax (built-in regex only). */
|
|
|
|
wxRE_ADVANCED = 1,
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2008-06-20 22:38:44 -04:00
|
|
|
@anchor wxRE_NOT_FLAGS
|
|
|
|
|
2008-06-21 01:53:53 -04:00
|
|
|
Flags for regex matching to be used with wxRegEx::Matches().
|
2008-05-04 05:04:38 -04:00
|
|
|
These flags are mainly useful when doing several matches in a long string
|
2008-06-21 01:53:53 -04:00
|
|
|
to prevent erroneous matches for '^' and '$':
|
2008-05-04 05:04:38 -04:00
|
|
|
*/
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/** '^' doesn't match at the start of line. */
|
|
|
|
wxRE_NOTBOL = 32,
|
|
|
|
|
|
|
|
/** '$' doesn't match at the end of line. */
|
|
|
|
wxRE_NOTEOL = 64
|
|
|
|
};
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxRegEx
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
wxRegEx represents a regular expression. This class provides support
|
|
|
|
for regular expressions matching and also replacement.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
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.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Regular expressions, as defined by POSIX, come in two flavours: @e extended
|
|
|
|
and @e basic. The builtin library also adds a third flavour
|
2008-05-04 05:04:38 -04:00
|
|
|
of expression @ref overview_resyntax "advanced", which is not available
|
2008-03-08 08:52:38 -05:00
|
|
|
when using the system library.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
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.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
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.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
|
|
|
@category{data}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-05-04 05:04:38 -04:00
|
|
|
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
|
|
|
|
@c user@host.net):
|
|
|
|
|
|
|
|
@code
|
|
|
|
wxString text;
|
|
|
|
...
|
2009-01-18 16:46:46 -05:00
|
|
|
wxRegEx reEmail = "([^@]+)@([[:alnum:].-_].)+([[:alnum:]]+)";
|
2008-05-04 05:04:38 -04:00
|
|
|
if ( reEmail.Matches(text) )
|
|
|
|
{
|
|
|
|
wxString text = reEmail.GetMatch(email);
|
|
|
|
wxString username = reEmail.GetMatch(email, 1);
|
2009-01-18 16:46:46 -05:00
|
|
|
if ( reEmail.GetMatch(email, 3) == "com" ) // .com TLD?
|
2008-05-04 05:04:38 -04:00
|
|
|
{
|
|
|
|
...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// or we could do this to hide the email address
|
2009-01-18 16:46:46 -05:00
|
|
|
size_t count = reEmail.ReplaceAll(text, "HIDDEN@\\2\\3");
|
2008-05-04 05:04:38 -04:00
|
|
|
printf("text now contains %u hidden addresses", count);
|
|
|
|
@endcode
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxRegEx
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
2008-05-04 05:04:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Default constructor: use Compile() later.
|
|
|
|
*/
|
|
|
|
wxRegEx();
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Create and compile the regular expression, use
|
2008-03-08 08:52:38 -05:00
|
|
|
IsValid() to test for compilation errors.
|
2008-05-04 05:04:38 -04:00
|
|
|
|
2008-06-20 22:38:44 -04:00
|
|
|
As for the flags, please see @ref wxRE_FLAGS.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
wxRegEx(const wxString& expr, int flags = wxRE_DEFAULT);
|
2008-05-04 05:04:38 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-05-04 05:04:38 -04:00
|
|
|
Destructor. It's not virtual, don't derive from this class.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
~wxRegEx();
|
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Compile the string into regular expression, return @true if ok or @false
|
2008-03-08 08:52:38 -05:00
|
|
|
if string has a syntax error.
|
2008-05-04 05:04:38 -04:00
|
|
|
|
2008-06-20 22:38:44 -04:00
|
|
|
As for the flags, please see @ref wxRE_FLAGS.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
bool Compile(const wxString& pattern, int flags = wxRE_DEFAULT);
|
|
|
|
|
|
|
|
/**
|
2008-05-04 05:04:38 -04:00
|
|
|
Get the start index and the length of the match of the expression
|
|
|
|
(if @a index is 0) or a bracketed subexpression (@a index different from 0).
|
|
|
|
|
|
|
|
May only be called after successful call to Matches() and only if @c wxRE_NOSUB
|
|
|
|
was @b not used in Compile().
|
|
|
|
|
|
|
|
Returns @false if no match or if an error occurred.
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool GetMatch(size_t* start, size_t* len, size_t index = 0) const;
|
2008-05-04 05:04:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
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 @c wxRE_NOSUB
|
|
|
|
was @b not used in Compile().
|
|
|
|
*/
|
|
|
|
wxString GetMatch(const wxString& text, size_t index = 0) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
2008-05-04 05:04:38 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
May only be called after successful call to Compile().
|
|
|
|
and only if @c wxRE_NOSUB was @b not used.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
size_t GetMatchCount() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Return @true if this is a valid compiled regular expression, @false
|
2008-03-08 08:52:38 -05:00
|
|
|
otherwise.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsValid() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
//@{
|
|
|
|
/**
|
2008-05-04 05:04:38 -04:00
|
|
|
Matches the precompiled regular expression against the string @a text,
|
2008-03-08 08:52:38 -05:00
|
|
|
returns @true if matches and @false otherwise.
|
2008-05-04 05:04:38 -04:00
|
|
|
|
2008-06-20 22:38:44 -04:00
|
|
|
@e Flags may be combination of @c wxRE_NOTBOL and @c wxRE_NOTEOL, see
|
|
|
|
@ref wxRE_NOT_FLAGS.
|
2008-05-04 05:04:38 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Some regex libraries assume that the text given is null terminated, while
|
|
|
|
others require the length be given as a separate parameter. Therefore for
|
2008-03-09 08:33:59 -04:00
|
|
|
maximum portability assume that @a text cannot contain embedded nulls.
|
2008-05-04 05:04:38 -04:00
|
|
|
|
|
|
|
When the <b>Matches(const wxChar *text, int flags = 0)</b> form is used,
|
|
|
|
a wxStrlen() will be done internally if the regex library requires the
|
|
|
|
length. When using Matches() in a loop the <b>Matches(text, flags, len)</b>
|
|
|
|
form can be used instead, making it possible to avoid a wxStrlen() inside
|
|
|
|
the loop.
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
May only be called after successful call to Compile().
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool Matches(const wxChar* text, int flags = 0) const;
|
2008-11-13 16:32:53 -05:00
|
|
|
bool Matches(const wxChar* text, int flags, size_t len) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
//@}
|
|
|
|
|
2008-05-04 05:04:38 -04:00
|
|
|
/**
|
|
|
|
Matches the precompiled regular expression against the string @a text,
|
|
|
|
returns @true if matches and @false otherwise.
|
|
|
|
|
2008-06-20 22:38:44 -04:00
|
|
|
@e Flags may be combination of @c wxRE_NOTBOL and @c wxRE_NOTEOL, see
|
|
|
|
@ref wxRE_NOT_FLAGS.
|
2008-05-04 05:04:38 -04:00
|
|
|
|
|
|
|
May only be called after successful call to Compile().
|
|
|
|
*/
|
2008-11-13 16:32:53 -05:00
|
|
|
bool Matches(const wxString& text, int flags = 0) const;
|
2008-05-04 05:04:38 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
Replaces the current regular expression in the string pointed to by
|
2008-05-04 05:04:38 -04:00
|
|
|
@a text, with the text in @a replacement and return number of matches
|
2008-03-08 08:52:38 -05:00
|
|
|
replaced (maybe 0 if none found) or -1 on error.
|
2008-05-04 05:04:38 -04:00
|
|
|
|
|
|
|
The replacement text may contain back references @c \\number which will be
|
2008-03-08 08:52:38 -05:00
|
|
|
replaced with the value of the corresponding subexpression in the
|
2008-05-04 05:04:38 -04:00
|
|
|
pattern match. @c \\0 corresponds to the entire match and @c \& is a
|
|
|
|
synonym for it. Backslash may be used to quote itself or @c \& character.
|
|
|
|
|
2008-03-09 08:33:59 -04:00
|
|
|
@a maxMatches may be used to limit the number of replacements made, setting
|
2008-03-08 08:52:38 -05:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
int Replace(wxString* text, const wxString& replacement,
|
2008-03-09 12:24:26 -04:00
|
|
|
size_t maxMatches = 0) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Replace all occurrences: this is actually a synonym for
|
2008-03-08 08:52:38 -05:00
|
|
|
Replace().
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-09 08:33:59 -04:00
|
|
|
@see ReplaceFirst()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
int ReplaceAll(wxString* text, const wxString& replacement) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Replace the first occurrence.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
int ReplaceFirst(wxString* text, const wxString& replacement) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|