]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/regex.h
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     interface of wxRegEx 
   4 // Author:      wxWidgets team 
   6 // Licence:     wxWindows license 
   7 ///////////////////////////////////////////////////////////////////////////// 
  12     Flags for regex compilation to be used with wxRegEx::Compile(). 
  16     /** Use extended regex syntax. */ 
  19     /** Use advanced RE syntax (built-in regex only). */ 
  22     /** Use basic RE syntax. */ 
  25     /** Ignore case in match. */ 
  28     /** Only check match, don't set back references. */ 
  32         If not set, treat '\n' as an ordinary character, otherwise it is 
  33         special: it is not matched by '.' and '^' and '$' always match 
  34         after/before it regardless of the setting of wxRE_NOT[BE]OL. 
  39     wxRE_DEFAULT  
= wxRE_EXTENDED
 
  43     @anchor wxRE_NOT_FLAGS 
  45     Flags for regex matching to be used with wxRegEx::Matches(). 
  46     These flags are mainly useful when doing several matches in a long string 
  47     to prevent erroneous matches for '^' and '$': 
  51     /** '^' doesn't match at the start of line. */ 
  54     /** '$' doesn't match at the end of line. */ 
  61     wxRegEx represents a regular expression.  This class provides support 
  62     for regular expressions matching and also replacement. 
  64     It is built on top of either the system library (if it has support 
  65     for POSIX regular expressions - which is the case of the most modern 
  66     Unices) or uses the built in Henry Spencer's library.  Henry Spencer 
  67     would appreciate being given credit in the documentation of software 
  68     which uses his library, but that is not a requirement. 
  70     Regular expressions, as defined by POSIX, come in two flavours: @e extended 
  71     and @e basic.  The builtin library also adds a third flavour 
  72     of expression @ref overview_resyntax "advanced", which is not available 
  73     when using the system library. 
  75     Unicode is fully supported only when using the builtin library. 
  76     When using the system library in Unicode mode, the expressions and data 
  77     are translated to the default 8-bit encoding before being passed to 
  80     On platforms where a system library is available, the default is to use 
  81     the builtin library for Unicode builds, and the system library otherwise. 
  82     It is possible to use the other if preferred by selecting it when building 
  90     A bad example of processing some text containing email addresses (the example 
  91     is bad because the real email addresses can have more complicated form than 
  97     wxRegEx reEmail = wxT("([^@]+)@([[:alnum:].-_].)+([[:alnum:]]+)"); 
  98     if ( reEmail.Matches(text) ) 
 100         wxString text = reEmail.GetMatch(email); 
 101         wxString username = reEmail.GetMatch(email, 1); 
 102         if ( reEmail.GetMatch(email, 3) == wxT("com") ) // .com TLD? 
 108     // or we could do this to hide the email address 
 109     size_t count = reEmail.ReplaceAll(text, wxT("HIDDEN@\\2\\3")); 
 110     printf("text now contains %u hidden addresses", count); 
 118         Default constructor: use Compile() later. 
 123         Create and compile the regular expression, use 
 124         IsValid() to test for compilation errors. 
 126         As for the flags, please see @ref wxRE_FLAGS. 
 128     wxRegEx(const wxString
& expr
, int flags 
= wxRE_DEFAULT
); 
 132         Destructor. It's not virtual, don't derive from this class. 
 137         Compile the string into regular expression, return @true if ok or @false 
 138         if string has a syntax error. 
 140         As for the flags, please see @ref wxRE_FLAGS. 
 142     bool Compile(const wxString
& pattern
, int flags 
= wxRE_DEFAULT
); 
 145         Get the start index and the length of the match of the expression 
 146         (if @a index is 0) or a bracketed subexpression (@a index different from 0). 
 148         May only be called after successful call to Matches() and only if @c wxRE_NOSUB 
 149         was @b not used in Compile(). 
 151         Returns @false if no match or if an error occurred. 
 154     bool GetMatch(size_t* start
, size_t* len
, size_t index 
= 0) const; 
 157         Returns the part of string corresponding to the match where index is interpreted 
 158         as above. Empty string is returned if match failed. 
 160         May only be called after successful call to Matches() and only if @c wxRE_NOSUB 
 161         was @b not used in Compile(). 
 163     wxString  
GetMatch(const wxString
& text
, size_t index 
= 0) const; 
 166         Returns the size of the array of matches, i.e. the number of bracketed 
 167         subexpressions plus one for the expression itself, or 0 on error. 
 169         May only be called after successful call to Compile(). 
 170         and only if @c wxRE_NOSUB was @b not used. 
 172     size_t GetMatchCount() const; 
 175         Return @true if this is a valid compiled regular expression, @false 
 178     bool IsValid() const; 
 182         Matches the precompiled regular expression against the string @a text, 
 183         returns @true if matches and @false otherwise. 
 185         @e Flags may be combination of @c wxRE_NOTBOL and @c wxRE_NOTEOL, see 
 188         Some regex libraries assume that the text given is null terminated, while 
 189         others require the length be given as a separate parameter. Therefore for 
 190         maximum portability assume that @a text cannot contain embedded nulls. 
 192         When the <b>Matches(const wxChar *text, int flags = 0)</b> form is used, 
 193         a wxStrlen() will be done internally if the regex library requires the 
 194         length. When using Matches() in a loop the <b>Matches(text, flags, len)</b> 
 195         form can be used instead, making it possible to avoid a wxStrlen() inside 
 198         May only be called after successful call to Compile(). 
 200     bool Matches(const wxChar
* text
, int flags 
= 0) const; 
 201     bool Matches(const wxChar
* text
, int flags
, size_t len
) const; 
 205         Matches the precompiled regular expression against the string @a text, 
 206         returns @true if matches and @false otherwise. 
 208         @e Flags may be combination of @c wxRE_NOTBOL and @c wxRE_NOTEOL, see 
 211         May only be called after successful call to Compile(). 
 213     bool Matches(const wxString
& text
, int flags 
= 0) const; 
 216         Replaces the current regular expression in the string pointed to by 
 217         @a text, with the text in @a replacement and return number of matches 
 218         replaced (maybe 0 if none found) or -1 on error. 
 220         The replacement text may contain back references @c \\number which will be 
 221         replaced with the value of the corresponding subexpression in the 
 222         pattern match. @c \\0 corresponds to the entire match and @c \& is a 
 223         synonym for it. Backslash may be used to quote itself or @c \& character. 
 225         @a maxMatches may be used to limit the number of replacements made, setting 
 226         it to 1, for example, will only replace first occurrence (if any) of the 
 227         pattern in the text while default value of 0 means replace all. 
 229     int Replace(wxString
* text
, const wxString
& replacement
, 
 230                 size_t maxMatches 
= 0) const; 
 233         Replace all occurrences: this is actually a synonym for 
 238     int ReplaceAll(wxString
* text
, const wxString
& replacement
) const; 
 241         Replace the first occurrence. 
 243     int ReplaceFirst(wxString
* text
, const wxString
& replacement
) const;