]> git.saurik.com Git - wxWidgets.git/blob - include/wx/regex.h
Change SetStringSelection to return bool like in other classes and ports
[wxWidgets.git] / include / wx / regex.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/regex.h
3 // Purpose: regular expression matching
4 // Author: Karsten Ballüder
5 // Modified by: VZ at 13.07.01 (integrated to wxWin)
6 // Created: 05.02.2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Karsten Ballüder <ballueder@gmx.net>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_REGEX_H_
13 #define _WX_REGEX_H_
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "regex.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_REGEX
22
23 class WXDLLIMPEXP_BASE wxString;
24
25 // ----------------------------------------------------------------------------
26 // constants
27 // ----------------------------------------------------------------------------
28
29 // flags for regex compilation: these can be used with Compile()
30 enum
31 {
32 // use extended regex syntax
33 wxRE_EXTENDED = 0,
34
35 // use advanced RE syntax (built-in regex only)
36 #ifdef wxHAS_REGEX_ADVANCED
37 wxRE_ADVANCED = 1,
38 #endif
39
40 // use basic RE syntax
41 wxRE_BASIC = 2,
42
43 // ignore case in match
44 wxRE_ICASE = 4,
45
46 // only check match, don't set back references
47 wxRE_NOSUB = 8,
48
49 // if not set, treat '\n' as an ordinary character, otherwise it is
50 // special: it is not matched by '.' and '^' and '$' always match
51 // after/before it regardless of the setting of wxRE_NOT[BE]OL
52 wxRE_NEWLINE = 16,
53
54 // default flags
55 wxRE_DEFAULT = wxRE_EXTENDED
56 };
57
58 // flags for regex matching: these can be used with Matches()
59 //
60 // these flags are mainly useful when doing several matches in a long string,
61 // they can be used to prevent erroneous matches for '^' and '$'
62 enum
63 {
64 // '^' doesn't match at the start of line
65 wxRE_NOTBOL = 32,
66
67 // '$' doesn't match at the end of line
68 wxRE_NOTEOL = 64
69 };
70
71 // ----------------------------------------------------------------------------
72 // wxRegEx: a regular expression
73 // ----------------------------------------------------------------------------
74
75 class WXDLLIMPEXP_BASE wxRegExImpl;
76
77 class WXDLLIMPEXP_BASE wxRegEx
78 {
79 public:
80 // default ctor: use Compile() later
81 wxRegEx() { Init(); }
82
83 // create and compile
84 wxRegEx(const wxString& expr, int flags = wxRE_DEFAULT)
85 {
86 Init();
87 (void)Compile(expr, flags);
88 }
89
90 // return TRUE if this is a valid compiled regular expression
91 bool IsValid() const { return m_impl != NULL; }
92
93 // compile the string into regular expression, return TRUE if ok or FALSE
94 // if string has a syntax error
95 bool Compile(const wxString& pattern, int flags = wxRE_DEFAULT);
96
97 // matches the precompiled regular expression against a string, return
98 // TRUE if matches and FALSE otherwise
99 //
100 // flags may be combination of wxRE_NOTBOL and wxRE_NOTEOL
101 //
102 // may only be called after successful call to Compile()
103 bool Matches(const wxChar *text, int flags = 0) const;
104
105 // get the start index and the length of the match of the expression
106 // (index 0) or a bracketed subexpression (index != 0)
107 //
108 // may only be called after successful call to Matches()
109 //
110 // return FALSE if no match or on error
111 bool GetMatch(size_t *start, size_t *len, size_t index = 0) const;
112
113 // return the part of string corresponding to the match, empty string is
114 // returned if match failed
115 //
116 // may only be called after successful call to Matches()
117 wxString GetMatch(const wxString& text, size_t index = 0) const;
118
119 // return the size of the array of matches, i.e. the number of bracketed
120 // subexpressions plus one for the expression itself, or 0 on error.
121 //
122 // may only be called after successful call to Compile()
123 size_t GetMatchCount() const;
124
125 // replaces the current regular expression in the string pointed to by
126 // pattern, with the text in replacement and return number of matches
127 // replaced (maybe 0 if none found) or -1 on error
128 //
129 // the replacement text may contain backreferences (\number) which will be
130 // replaced with the value of the corresponding subexpression in the
131 // pattern match
132 //
133 // maxMatches may be used to limit the number of replacements made, setting
134 // it to 1, for example, will only replace first occurence (if any) of the
135 // pattern in the text while default value of 0 means replace all
136 int Replace(wxString *text, const wxString& replacement,
137 size_t maxMatches = 0) const;
138
139 // replace the first occurence
140 int ReplaceFirst(wxString *text, const wxString& replacement) const
141 { return Replace(text, replacement, 1); }
142
143 // replace all occurences: this is actually a synonym for Replace()
144 int ReplaceAll(wxString *text, const wxString& replacement) const
145 { return Replace(text, replacement, 0); }
146
147 // dtor not virtual, don't derive from this class
148 ~wxRegEx();
149
150 private:
151 // common part of all ctors
152 void Init();
153
154 // the real guts of this class
155 wxRegExImpl *m_impl;
156
157 // as long as the class wxRegExImpl is not ref-counted,
158 // instances of the handle wxRegEx must not be copied.
159 wxRegEx(const wxRegEx&);
160 wxRegEx &operator=(const wxRegEx&);
161 };
162
163 #endif // wxUSE_REGEX
164
165 #endif // _WX_REGEX_H_
166