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. The regular expressions syntax
15 supported is the POSIX one. Both basic and extended regular expressions are
16 supported but, unlike POSIX C API, the extended ones are used by default.
18 This class provides support for regular expressions matching and also
19 replacement. It is built on top of either the system library (if it has support
20 for POSIX regular expressions - which is the case of the most modern Unices) or
21 uses the built-in Henry Spencer's library from tcl.
23 \wxheading{Derived from
}
27 \wxheading{Data structures
}
29 Flags for regex compilation to be used with
\helpref{Compile()
}{wxregexcompile
}:
34 // use extended regex syntax (default)
37 // use basic RE syntax
40 // ignore case in match
43 // only check match, don't set back references
46 // if not set, treat '
\n' as an ordinary character, otherwise it is
47 // special: it is not matched by '.' and '^' and '$' always match
48 // after/before it regardless of the setting of wxRE_NOT
[BE
]OL
52 wxRE_DEFAULT = wxRE_EXTENDED
56 Flags for regex matching to be used with
\helpref{Matches()
}{wxregexmatches
}.
58 These flags are mainly useful when doing several matches in a long string
59 to prevent erroneous matches for
{\tt '
\textasciicircum'
} and
{\tt '\$'
}:
64 // '^' doesn't match at the start of line
67 // '$' doesn't match at the end of line
74 A bad example of processing some text containing email addresses (the example
75 is bad because the real email addresses can have more complicated form than
81 wxRegEx reEmail = "(
[^@
]+)@(
[[:alnum:
].-_
].)+(
[[:alnum:
]]+)";
82 if ( reEmail.Matches(text) )
84 wxString text = reEmail.GetMatch(email);
85 wxString username = reEmail.GetMatch(email,
1);
86 if ( reEmail.GetMatch(email,
3) == "com" ) // .com TLD?
92 // or we could do this to hide the email address
93 size_t count = reEmail.ReplaceAll(text, "HIDDEN@\
\2\
\3");
94 printf("text now contains
%u hidden addresses", count);
97 \latexignore{\rtfignore{\wxheading{Members
}}}
99 \membersection{wxRegEx::wxRegEx
}\label{wxregexwxregex
}
101 \func{}{wxRegEx
}{\void}
103 Default ctor: use
\helpref{Compile()
}{wxregexcompile
} later.
105 \func{}{wxRegEx
}{\param{const wxString\&
}{expr
},
\param{int
}{flags = wxRE
\_DEFAULT}}
107 Create and compile the regular expression, use
108 \helpref{IsValid
}{wxregexisvalid
} to test for compilation errors.
110 \membersection{wxRegEx::
\destruct{wxRegEx
}}\label{wxregexdtor
}
112 \func{}{\destruct{wxRegEx
}}{\void}
114 dtor not virtual, don't derive from this class
116 \membersection{wxRegEx::Compile
}\label{wxregexcompile
}
118 \func{bool
}{Compile
}{\param{const wxString\&
}{pattern
},
\param{int
}{flags = wxRE
\_DEFAULT}}
120 Compile the string into regular expression, return
{\tt true
} if ok or
{\tt false
}
121 if string has a syntax error.
123 \membersection{wxRegEx::IsValid
}\label{wxregexisvalid
}
125 \constfunc{bool
}{IsValid
}{\void}
127 Return
{\tt true
} if this is a valid compiled regular expression,
{\tt false
}
130 \membersection{wxRegEx::GetMatch
}\label{wxregexgetmatch
}
132 \constfunc{bool
}{GetMatch
}{\param{size
\_t*
}{start
},
\param{size
\_t*
}{len
},
\param{size
\_t }{index =
0}}
134 Get the start index and the length of the match of the expression
135 (if
{\it index
} is $
0$) or a bracketed subexpression (
{\it index
} different
138 May only be called after successful call to
\helpref{Matches()
}{wxregexmatches
}
139 and only if
{\tt wxRE
\_NOSUB} was
{\bf not
} used in
140 \helpref{Compile()
}{wxregexcompile
}.
142 Returns
{\tt false
} if no match or if an error occured.
144 \constfunc{wxString
}{GetMatch
}{\param{const wxString\&
}{text
},
\param{size
\_t }{index =
0}}
146 Returns the part of string corresponding to the match where
{\it index
} is
147 interpreted as above. Empty string is returned if match failed
149 May only be called after successful call to
\helpref{Matches()
}{wxregexmatches
}
150 and only if
{\tt wxRE
\_NOSUB} was
{\bf not
} used in
151 \helpref{Compile()
}{wxregexcompile
}.
153 \membersection{wxRegEx::Matches
}\label{wxregexmatches
}
155 \constfunc{bool
}{Matches
}{\param{const wxChar*
}{text
},
\param{int
}{flags =
0}}
157 Matches the precompiled regular expression against the string
{\it text
},
158 returns
{\tt true
} if matches and
{\tt false
} otherwise.
160 Flags may be combination of
{\tt wxRE
\_NOTBOL} and
{\tt wxRE
\_NOTEOL}.
162 May only be called after successful call to
\helpref{Compile()
}{wxregexcompile
}.
164 \membersection{wxRegEx::Replace
}\label{wxregexreplace
}
166 \constfunc{int
}{Replace
}{\param{wxString*
}{text
},
\param{const wxString\&
}{replacement
},
\param{size
\_t }{maxMatches =
0}}
168 Replaces the current regular expression in the string pointed to by
169 {\it text
}, with the text in
{\it replacement
} and return number of matches
170 replaced (maybe $
0$ if none found) or $-
1$ on error.
172 The replacement text may contain back references
{\tt $
\backslash$number
} which will be
173 replaced with the value of the corresponding subexpression in the
174 pattern match.
{\tt $
\backslash$
0} corresponds to the entire match and
{\tt \&
} is a
175 synonym for it. Backslash may be used to quote itself or
{\tt \&
} character.
177 {\it maxMatches
} may be used to limit the number of replacements made, setting
178 it to $
1$, for example, will only replace first occurrence (if any) of the
179 pattern in the text while default value of $
0$ means replace all.
181 \membersection{wxRegEx::ReplaceAll
}\label{wxregexreplaceall
}
183 \constfunc{int
}{ReplaceAll
}{\param{wxString*
}{text
},
\param{const wxString\&
}{replacement
}}
185 Replace all occurrences: this is actually a synonym for
186 \helpref{Replace()
}{wxregexreplace
}.
190 \helpref{ReplaceFirst
}{wxregexreplacefirst
}
192 \membersection{wxRegEx::ReplaceFirst
}\label{wxregexreplacefirst
}
194 \constfunc{int
}{ReplaceFirst
}{\param{wxString*
}{text
},
\param{const wxString\&
}{replacement
}}
196 Replace the first occurrence.
200 \helpref{Replace
}{wxregexreplace
}