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