1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% Purpose: wxRegEx documentation
4 %% Author: Vadim Zeitlin
8 %% Copyright: (c) 2001 Vadim Zeitlin
9 %% License: wxWindows license
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 \section{\class{wxRegEx
}}\label{wxregex
}
14 wxRegEx represents a regular expression. This class provides support
15 for regular expressions matching and also replacement.
17 It is built on top of either the system library (if it has support
18 for POSIX regular expressions - which is the case of the most modern
19 Unices) or uses the built in Henry Spencer's library. Henry Spencer
20 would appreciate being given credit in the documentation of software
21 which uses his library, but that is not a requirement.
23 Regular expressions, as defined by POSIX, come in two flavours:
{\it extended
}
24 and
{\it basic
}. The builtin library also adds a third flavour
25 of expression
\helpref{advanced
}{wxresyn
}, which is not available
26 when using the system library.
28 Unicode is fully supported only when using the builtin library.
29 When using the system library in Unicode mode, the expressions and data
30 are translated to the default
8-bit encoding before being passed to
33 On platforms where a system library is available, the default is to use
34 the builtin library for Unicode builds, and the system library otherwise.
35 It is possible to use the other if preferred by selecting it when building
38 \wxheading{Derived from
}
42 \wxheading{Data structures
}
44 Flags for regex compilation to be used with
\helpref{Compile()
}{wxregexcompile
}:
49 // use extended regex syntax
52 // use advanced RE syntax (built-in regex only)
53 #ifdef wxHAS_REGEX_ADVANCED
57 // use basic RE syntax
60 // ignore case in match
63 // only check match, don't set back references
66 // if not set, treat '
\n' as an ordinary character, otherwise it is
67 // special: it is not matched by '.' and '^' and '$' always match
68 // after/before it regardless of the setting of wxRE_NOT
[BE
]OL
72 wxRE_DEFAULT = wxRE_EXTENDED
76 Flags for regex matching to be used with
\helpref{Matches()
}{wxregexmatches
}.
78 These flags are mainly useful when doing several matches in a long string
79 to prevent erroneous matches for
{\tt '
\textasciicircum'
} and
{\tt '\$'
}:
84 // '^' doesn't match at the start of line
87 // '$' doesn't match at the end of line
94 A bad example of processing some text containing email addresses (the example
95 is bad because the real email addresses can have more complicated form than
101 wxRegEx reEmail = wxT("(
[^@
]+)@(
[[:alnum:
].-_
].)+(
[[:alnum:
]]+)");
102 if ( reEmail.Matches(text) )
104 wxString text = reEmail.GetMatch(email);
105 wxString username = reEmail.GetMatch(email,
1);
106 if ( reEmail.GetMatch(email,
3) == wxT("com") ) // .com TLD?
112 // or we could do this to hide the email address
113 size_t count = reEmail.ReplaceAll(text, wxT("HIDDEN@\
\2\
\3"));
114 printf("text now contains
%u hidden addresses", count);
117 \wxheading{Include files
}
123 \helpref{wxBase
}{librarieslist
}
125 \latexignore{\rtfignore{\wxheading{Members
}}}
127 \membersection{wxRegEx::wxRegEx
}\label{wxregexwxregex
}
129 \func{}{wxRegEx
}{\void}
131 Default ctor: use
\helpref{Compile()
}{wxregexcompile
} later.
133 \func{}{wxRegEx
}{\param{const wxString\&
}{expr
},
\param{int
}{flags = wxRE
\_DEFAULT}}
135 Create and compile the regular expression, use
136 \helpref{IsValid
}{wxregexisvalid
} to test for compilation errors.
138 \membersection{wxRegEx::
\destruct{wxRegEx
}}\label{wxregexdtor
}
140 \func{}{\destruct{wxRegEx
}}{\void}
142 dtor not virtual, don't derive from this class
144 \membersection{wxRegEx::Compile
}\label{wxregexcompile
}
146 \func{bool
}{Compile
}{\param{const wxString\&
}{pattern
},
\param{int
}{flags = wxRE
\_DEFAULT}}
148 Compile the string into regular expression, return
{\tt true
} if ok or
{\tt false
}
149 if string has a syntax error.
151 \membersection{wxRegEx::IsValid
}\label{wxregexisvalid
}
153 \constfunc{bool
}{IsValid
}{\void}
155 Return
{\tt true
} if this is a valid compiled regular expression,
{\tt false
}
158 \membersection{wxRegEx::GetMatch
}\label{wxregexgetmatch
}
160 \constfunc{bool
}{GetMatch
}{\param{size
\_t*
}{start
},
\param{size
\_t*
}{len
},
\param{size
\_t }{index =
0}}
162 Get the start index and the length of the match of the expression
163 (if
{\it index
} is $
0$) or a bracketed subexpression (
{\it index
} different
166 May only be called after successful call to
\helpref{Matches()
}{wxregexmatches
}
167 and only if
{\tt wxRE
\_NOSUB} was
{\bf not
} used in
168 \helpref{Compile()
}{wxregexcompile
}.
170 Returns
{\tt false
} if no match or if an error occurred.
172 \constfunc{wxString
}{GetMatch
}{\param{const wxString\&
}{text
},
\param{size
\_t }{index =
0}}
174 Returns the part of string corresponding to the match where
{\it index
} is
175 interpreted as above. Empty string is returned if match failed
177 May only be called after successful call to
\helpref{Matches()
}{wxregexmatches
}
178 and only if
{\tt wxRE
\_NOSUB} was
{\bf not
} used in
179 \helpref{Compile()
}{wxregexcompile
}.
181 \membersection{wxRegEx::GetMatchCount
}\label{wxregexgetmatchcount
}
183 \constfunc{size
\_t}{GetMatchCount
}{\void}
185 Returns the size of the array of matches, i.e. the number of bracketed
186 subexpressions plus one for the expression itself, or $
0$ on error.
188 May only be called after successful call to
\helpref{Compile()
}{wxregexcompile
}.
189 and only if
{\tt wxRE
\_NOSUB} was
{\bf not
} used.
191 \membersection{wxRegEx::Matches
}\label{wxregexmatches
}
193 \constfunc{bool
}{Matches
}{\param{const wxChar*
}{text
},
\param{int
}{flags =
0}}
195 \constfunc{bool
}{Matches
}{\param{const wxChar*
}{text
},
\param{int
}{flags
},
\param{size
\_t }{len
}}
197 \constfunc{bool
}{Matches
}{\param{const wxString\&
}{text
},
\param{int
}{flags =
0}}
199 Matches the precompiled regular expression against the string
{\it text
},
200 returns
{\tt true
} if matches and
{\tt false
} otherwise.
202 {\it Flags
} may be combination of
{\tt wxRE
\_NOTBOL} and
{\tt wxRE
\_NOTEOL}.
204 Some regex libraries assume that the text given is null terminated, while
205 others require the length be given as a separate parameter. Therefore for
206 maximum portability assume that
{\it text
} cannot contain embedded nulls.
208 When the
{\it Matches(const wxChar *text, int flags =
0)
} form is used,
209 a
{\it wxStrlen()
} will be done internally if the regex library requires the
210 length. When using
{\it Matches()
} in a loop
211 the
{\it Matches(text, flags, len)
} form can be used instead, making it
212 possible to avoid a
{\it wxStrlen()
} inside the loop.
214 May only be called after successful call to
\helpref{Compile()
}{wxregexcompile
}.
216 \membersection{wxRegEx::Replace
}\label{wxregexreplace
}
218 \constfunc{int
}{Replace
}{\param{wxString*
}{text
},
\param{const wxString\&
}{replacement
},
\param{size
\_t }{maxMatches =
0}}
220 Replaces the current regular expression in the string pointed to by
221 {\it text
}, with the text in
{\it replacement
} and return number of matches
222 replaced (maybe $
0$ if none found) or $-
1$ on error.
224 The replacement text may contain back references
{\tt $
\backslash$number
} which will be
225 replaced with the value of the corresponding subexpression in the
226 pattern match.
{\tt $
\backslash$
0} corresponds to the entire match and
{\tt \&
} is a
227 synonym for it. Backslash may be used to quote itself or
{\tt \&
} character.
229 {\it maxMatches
} may be used to limit the number of replacements made, setting
230 it to $
1$, for example, will only replace first occurrence (if any) of the
231 pattern in the text while default value of $
0$ means replace all.
233 \membersection{wxRegEx::ReplaceAll
}\label{wxregexreplaceall
}
235 \constfunc{int
}{ReplaceAll
}{\param{wxString*
}{text
},
\param{const wxString\&
}{replacement
}}
237 Replace all occurrences: this is actually a synonym for
238 \helpref{Replace()
}{wxregexreplace
}.
242 \helpref{ReplaceFirst
}{wxregexreplacefirst
}
244 \membersection{wxRegEx::ReplaceFirst
}\label{wxregexreplacefirst
}
246 \constfunc{int
}{ReplaceFirst
}{\param{wxString*
}{text
},
\param{const wxString\&
}{replacement
}}
248 Replace the first occurrence.
252 \helpref{Replace
}{wxregexreplace
}