]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: regex.h | |
e54c96f1 | 3 | // Purpose: interface of wxRegEx |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxRegEx | |
11 | @wxheader{regex.h} | |
7c913512 | 12 | |
23324ae1 FM |
13 | wxRegEx represents a regular expression. This class provides support |
14 | for regular expressions matching and also replacement. | |
7c913512 | 15 | |
23324ae1 FM |
16 | It is built on top of either the system library (if it has support |
17 | for POSIX regular expressions - which is the case of the most modern | |
18 | Unices) or uses the built in Henry Spencer's library. Henry Spencer | |
19 | would appreciate being given credit in the documentation of software | |
20 | which uses his library, but that is not a requirement. | |
7c913512 | 21 | |
23324ae1 FM |
22 | Regular expressions, as defined by POSIX, come in two flavours: @e extended |
23 | and @e basic. The builtin library also adds a third flavour | |
e54c96f1 | 24 | of expression advanced(), which is not available |
23324ae1 | 25 | when using the system library. |
7c913512 | 26 | |
23324ae1 FM |
27 | Unicode is fully supported only when using the builtin library. |
28 | When using the system library in Unicode mode, the expressions and data | |
29 | are translated to the default 8-bit encoding before being passed to | |
30 | the library. | |
7c913512 | 31 | |
23324ae1 FM |
32 | On platforms where a system library is available, the default is to use |
33 | the builtin library for Unicode builds, and the system library otherwise. | |
34 | It is possible to use the other if preferred by selecting it when building | |
35 | the wxWidgets. | |
7c913512 | 36 | |
23324ae1 FM |
37 | @library{wxbase} |
38 | @category{data} | |
7c913512 | 39 | |
e54c96f1 | 40 | @see wxRegEx::ReplaceFirst |
23324ae1 | 41 | */ |
7c913512 | 42 | class wxRegEx |
23324ae1 FM |
43 | { |
44 | public: | |
45 | //@{ | |
46 | /** | |
7c913512 | 47 | Create and compile the regular expression, use |
23324ae1 FM |
48 | IsValid() to test for compilation errors. |
49 | */ | |
50 | wxRegEx(); | |
7c913512 | 51 | wxRegEx(const wxString& expr, int flags = wxRE_DEFAULT); |
23324ae1 FM |
52 | //@} |
53 | ||
54 | /** | |
55 | dtor not virtual, don't derive from this class | |
56 | */ | |
57 | ~wxRegEx(); | |
58 | ||
59 | /** | |
7c913512 | 60 | Compile the string into regular expression, return @true if ok or @false |
23324ae1 FM |
61 | if string has a syntax error. |
62 | */ | |
63 | bool Compile(const wxString& pattern, int flags = wxRE_DEFAULT); | |
64 | ||
65 | //@{ | |
66 | /** | |
4cc4bfaf | 67 | Returns the part of string corresponding to the match where @a index is |
23324ae1 | 68 | interpreted as above. Empty string is returned if match failed |
7c913512 FM |
69 | May only be called after successful call to Matches() |
70 | and only if @c wxRE_NOSUB was @b not used in | |
23324ae1 FM |
71 | Compile(). |
72 | */ | |
328f5751 FM |
73 | bool GetMatch(size_t* start, size_t* len, size_t index = 0) const; |
74 | const not used in | |
75 | Compile(). | |
76 | Returns false if no match or if an error occurred. | |
77 | wxString GetMatch(const wxString& text, size_t index = 0) const; | |
23324ae1 FM |
78 | //@} |
79 | ||
80 | /** | |
81 | Returns the size of the array of matches, i.e. the number of bracketed | |
82 | subexpressions plus one for the expression itself, or 0 on error. | |
23324ae1 FM |
83 | May only be called after successful call to Compile(). |
84 | and only if @c wxRE_NOSUB was @b not used. | |
85 | */ | |
328f5751 | 86 | size_t GetMatchCount() const; |
23324ae1 FM |
87 | |
88 | /** | |
7c913512 | 89 | Return @true if this is a valid compiled regular expression, @false |
23324ae1 FM |
90 | otherwise. |
91 | */ | |
328f5751 | 92 | bool IsValid() const; |
23324ae1 FM |
93 | |
94 | //@{ | |
95 | /** | |
96 | Matches the precompiled regular expression against the string @e text, | |
97 | returns @true if matches and @false otherwise. | |
23324ae1 | 98 | @e Flags may be combination of @c wxRE_NOTBOL and @c wxRE_NOTEOL. |
23324ae1 FM |
99 | Some regex libraries assume that the text given is null terminated, while |
100 | others require the length be given as a separate parameter. Therefore for | |
4cc4bfaf | 101 | maximum portability assume that @a text cannot contain embedded nulls. |
23324ae1 FM |
102 | When the @e Matches(const wxChar *text, int flags = 0) form is used, |
103 | a @e wxStrlen() will be done internally if the regex library requires the | |
104 | length. When using @e Matches() in a loop | |
105 | the @e Matches(text, flags, len) form can be used instead, making it | |
106 | possible to avoid a @e wxStrlen() inside the loop. | |
23324ae1 FM |
107 | May only be called after successful call to Compile(). |
108 | */ | |
328f5751 FM |
109 | bool Matches(const wxChar* text, int flags = 0) const; |
110 | const bool Matches(const wxChar* text, int flags, size_t len) const; | |
111 | const bool Matches(const wxString& text, int flags = 0) const; | |
23324ae1 FM |
112 | //@} |
113 | ||
114 | /** | |
115 | Replaces the current regular expression in the string pointed to by | |
4cc4bfaf | 116 | @e text, with the text in @a replacement and return number of matches |
23324ae1 | 117 | replaced (maybe 0 if none found) or -1 on error. |
23324ae1 FM |
118 | The replacement text may contain back references @c \number which will be |
119 | replaced with the value of the corresponding subexpression in the | |
120 | pattern match. @c \0 corresponds to the entire match and @c is a | |
121 | synonym for it. Backslash may be used to quote itself or @c character. | |
4cc4bfaf | 122 | @a maxMatches may be used to limit the number of replacements made, setting |
23324ae1 FM |
123 | it to 1, for example, will only replace first occurrence (if any) of the |
124 | pattern in the text while default value of 0 means replace all. | |
125 | */ | |
126 | int Replace(wxString* text, const wxString& replacement, | |
328f5751 | 127 | size_t maxMatches = 0) const; |
23324ae1 FM |
128 | |
129 | /** | |
7c913512 | 130 | Replace all occurrences: this is actually a synonym for |
23324ae1 FM |
131 | Replace(). |
132 | ||
4cc4bfaf | 133 | @see ReplaceFirst() |
23324ae1 | 134 | */ |
328f5751 | 135 | int ReplaceAll(wxString* text, const wxString& replacement) const; |
23324ae1 FM |
136 | |
137 | /** | |
138 | Replace the first occurrence. | |
139 | */ | |
328f5751 | 140 | int ReplaceFirst(wxString* text, const wxString& replacement) const; |
23324ae1 | 141 | }; |
e54c96f1 | 142 |