Tools.h++ Manual
104011 Tandem Computers Incorporated 5-5
5
5.4 Pattern matching
Class
RWCString
supports a convenient interface for string searches. Here is
an example. The code fragment:
RWCString s("curiouser and curiouser.");
int i = s.index("curious");
will find the start of the first occurrence of "
curious
" in
s
. The comparison
will be case sensitive. The result is that
i
will be set to “0”. To find the index
of the next occurrence use:
i = s.index("curious", ++i);
which will result in
i
being set to “14”. To make a case-insensitive comparison
use:
RWCString s("Curiouser and curiouser.");
int i = s.index("curious", 0, RWCString::ignoreCase);
which will also result in
i
being set to “0”.
If the pattern does not occur in the string, then
index()
will return the special
value
RW_NPOS
.
Regular expressions
The Tools.h++ Class Library supports regular expression searches. See Part 2:
Class Reference, under
RWCRegexp
, for details of the regular expression syntax.
A regular expression can be used to return a substring. Here's an example that
might be used to match all Windows messages (prefix of
WM_
):
#include <rw/cstring.h>
#include <rw/regexp.h>
#include <rw/rstream.h>
main()
{
RWCString a("A message named WM_CREATE");
// Construct a Regular Expression to match Windows messages:
RWCRegexp re("WM_[A-Z]*");
cout << a(re) << endl;