]> git.saurik.com Git - wxWidgets.git/blob - include/wx/regex.h
rewrote wxRegEx::Replace() to do something useful
[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 #ifdef __GNUG__
16 #pragma interface "regex.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_REGEX
22
23 // ----------------------------------------------------------------------------
24 // constants
25 // ----------------------------------------------------------------------------
26
27 // max number of subexpression matches, the default should be big enough for
28 // all uses but may be a bit wasteful
29 #ifndef WX_REGEX_MAXMATCHES
30 #define WX_REGEX_MAXMATCHES 1024
31 #endif
32
33 // flags for regex compilation: these can be used with Compile()
34 enum
35 {
36 // use extended regex syntax (default)
37 wxRE_EXTENDED = 0,
38
39 // use basic RE syntax
40 wxRE_BASIC = 2,
41
42 // ignore case in match
43 wxRE_ICASE = 4,
44
45 // only check match, don't set back references
46 wxRE_NOSUB = 8,
47
48 // if not set, treat '\n' as an ordinary character, otherwise it is
49 // special: it is not matched by '.' and '^' and '$' always match
50 // after/before it regardless of the setting of wxRE_NOT[BE]OL
51 wxRE_NEWLINE = 16,
52
53 // default flags
54 wxRE_DEFAULT = wxRE_EXTENDED
55 };
56
57 // flags for regex matching: these can be used with Matches()
58 //
59 // these flags are mainly useful when doing several matches in a long string,
60 // they can be used to prevent erroneous matches for '^' and '$'
61 enum
62 {
63 // '^' doesn't match at the start of line
64 wxRE_NOTBOL = 32,
65
66 // '$' doesn't match at the end of line
67 wxRE_NOTEOL = 64
68 };
69
70 // ----------------------------------------------------------------------------
71 // wxRegEx: a regular expression
72 // ----------------------------------------------------------------------------
73
74 class WXDLLEXPORT wxRegExImpl;
75
76 class WXDLLEXPORT wxRegEx
77 {
78 public:
79 // default ctor: use Compile() later
80 wxRegEx() { Init(); }
81
82 // create and compile
83 wxRegEx(const wxString& expr, int flags = wxRE_DEFAULT)
84 {
85 Init();
86 (void)Compile(expr, flags);
87 }
88
89 // return TRUE if this is a valid compiled regular expression
90 bool IsValid() const { return m_impl != NULL; }
91
92 // compile the string into regular expression, return TRUE if ok or FALSE
93 // if string has a syntax error
94 bool Compile(const wxString& pattern, int flags = wxRE_DEFAULT);
95
96 // matches the precompiled regular expression against a string, return
97 // TRUE if matches and FALSE otherwise
98 //
99 // flags may be combination of wxRE_NOTBOL and wxRE_NOTEOL
100 //
101 // may only be called after successful call to Compile()
102 bool Matches(const wxChar *text, int flags = 0) const;
103
104 // get the start index and the length of the match of the expression
105 // (index 0) or a bracketed subexpression (index != 0)
106 //
107 // may only be called after successful call to Matches()
108 //
109 // return FALSE if no match or on error
110 bool GetMatch(size_t *start, size_t *len, size_t index = 0) const;
111
112 // return the part of string corresponding to the match, empty string is
113 // returned if match failed
114 //
115 // may only be called after successful call to Matches()
116 wxString GetMatch(const wxString& text, size_t index = 0) const;
117
118 // replaces the current regular expression in the string pointed to by
119 // pattern, with the text in replacement and return number of matches
120 // replaced (maybe 0 if none found) or -1 on error
121 //
122 // the replacement text may contain backreferences (\number) which will be
123 // replaced with the value of the corresponding subexpression in the
124 // pattern match
125 //
126 // maxMatches may be used to limit the number of replacements made, setting
127 // it to 1, for example, will only replace first occurence (if any) of the
128 // pattern in the text while default value of 0 means replace all
129 int Replace(wxString *text, const wxString& replacement,
130 size_t maxMatches = 0) const;
131
132 // replace the first occurence
133 int ReplaceFirst(wxString *text, const wxString& replacement) const
134 { return Replace(text, replacement, 1); }
135
136 // replace all occurences: this is actually a synonym for Replace()
137 int ReplaceAll(wxString *text, const wxString& replacement) const
138 { return Replace(text, replacement, 0); }
139
140 // dtor not virtual, don't derive from this class
141 ~wxRegEx();
142
143 private:
144 // common part of all ctors
145 void Init();
146
147 // the real guts of this class
148 wxRegExImpl *m_impl;
149 };
150
151 #endif // wxUSE_REGEX
152
153 #endif // _WX_REGEX_H_
154