1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: regular expression matching
4 // Author: Karsten Ballüder
5 // Modified by: VZ at 13.07.01 (integrated to wxWin)
8 // Copyright: (c) 2000 Karsten Ballüder <ballueder@gmx.net>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
19 class WXDLLIMPEXP_BASE wxString
;
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // flags for regex compilation: these can be used with Compile()
28 // use extended regex syntax
31 // use advanced RE syntax (built-in regex only)
32 #ifdef wxHAS_REGEX_ADVANCED
36 // use basic RE syntax
39 // ignore case in match
42 // only check match, don't set back references
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
51 wxRE_DEFAULT
= wxRE_EXTENDED
54 // flags for regex matching: these can be used with Matches()
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 '$'
60 // '^' doesn't match at the start of line
63 // '$' doesn't match at the end of line
67 // ----------------------------------------------------------------------------
68 // wxRegEx: a regular expression
69 // ----------------------------------------------------------------------------
71 class WXDLLIMPEXP_BASE wxRegExImpl
;
73 class WXDLLIMPEXP_BASE wxRegEx
76 // default ctor: use Compile() later
80 wxRegEx(const wxString
& expr
, int flags
= wxRE_DEFAULT
)
83 (void)Compile(expr
, flags
);
86 // return true if this is a valid compiled regular expression
87 bool IsValid() const { return m_impl
!= NULL
; }
89 // compile the string into regular expression, return true if ok or false
90 // if string has a syntax error
91 bool Compile(const wxString
& pattern
, int flags
= wxRE_DEFAULT
);
93 // matches the precompiled regular expression against a string, return
94 // true if matches and false otherwise
96 // flags may be combination of wxRE_NOTBOL and wxRE_NOTEOL
98 // may only be called after successful call to Compile()
99 bool Matches(const wxChar
*text
, int flags
= 0) const;
101 // get the start index and the length of the match of the expression
102 // (index 0) or a bracketed subexpression (index != 0)
104 // may only be called after successful call to Matches()
106 // return false if no match or on error
107 bool GetMatch(size_t *start
, size_t *len
, size_t index
= 0) const;
109 // return the part of string corresponding to the match, empty string is
110 // returned if match failed
112 // may only be called after successful call to Matches()
113 wxString
GetMatch(const wxString
& text
, size_t index
= 0) const;
115 // return the size of the array of matches, i.e. the number of bracketed
116 // subexpressions plus one for the expression itself, or 0 on error.
118 // may only be called after successful call to Compile()
119 size_t GetMatchCount() const;
121 // replaces the current regular expression in the string pointed to by
122 // pattern, with the text in replacement and return number of matches
123 // replaced (maybe 0 if none found) or -1 on error
125 // the replacement text may contain backreferences (\number) which will be
126 // replaced with the value of the corresponding subexpression in the
129 // maxMatches may be used to limit the number of replacements made, setting
130 // it to 1, for example, will only replace first occurence (if any) of the
131 // pattern in the text while default value of 0 means replace all
132 int Replace(wxString
*text
, const wxString
& replacement
,
133 size_t maxMatches
= 0) const;
135 // replace the first occurence
136 int ReplaceFirst(wxString
*text
, const wxString
& replacement
) const
137 { return Replace(text
, replacement
, 1); }
139 // replace all occurences: this is actually a synonym for Replace()
140 int ReplaceAll(wxString
*text
, const wxString
& replacement
) const
141 { return Replace(text
, replacement
, 0); }
143 // dtor not virtual, don't derive from this class
147 // common part of all ctors
150 // the real guts of this class
153 // as long as the class wxRegExImpl is not ref-counted,
154 // instances of the handle wxRegEx must not be copied.
155 wxRegEx(const wxRegEx
&);
156 wxRegEx
&operator=(const wxRegEx
&);
159 #endif // wxUSE_REGEX
161 #endif // _WX_REGEX_H_