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