Class RWCString supports a convenient interface for string searches. In the example below, the code fragment:
RWCString s("curiouser and curiouser."); size_t i = s.index("curious");
will find the start of the first occurrence of curious in s. The comparison will be case sensitive, and the result will be that i is set to 0. To find the index of the next occurrence, you would use:
i = s.index("curious", ++i);
which will result in i set to 14. You can make a case-insensitive comparison with:
RWCString s("Curiouser and curiouser."); size_t i = s.index("curious", 0, RWCString::ignoreCase);
which will also result in i set to 0.
If the pattern does not occur in the string, the index() will return the special value RW_NPOS.
As part of its pattern matching capability, the Tools.h++ Class Library supports regular expression searches. See the Class Reference, under RWCRegexp, for details of the regular expression syntax. You can use a regular expression to return a substring; for example, here's how you might match all Windows messages (prefix 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; return 0; }
Program Output:
WM_CREATE
The function call operator for RWCString has been overloaded to take an argument of type RWCRegexp. It returns an RWCSubString matching the expression, or the null substring if there is no such expression.
This version of the Tools.h++ class library supports extended regular expression searches based on the POSIX.2 standard. (See the Bibliography.) Extended regular expressions are the regular expressions used in the UNIX utilities lex and awk. You will find details of the regular expression syntax in the Class Reference under RWCRExpr.
Note: RWCRExpr is available only if your compiler supports exception handling and the C++ Standard Library.
Extended regular expressions can be any length, although limited by available memory. You can use parentheses to group subexpressions, and the symbol _ to create either/or regular expressions for pattern matching.
The following example shows some of the capabilities of extended regular expressions:
#include "rw/rstream.h" #include "rw/re.h" main (){ RWCRExpr re("Lisa|Betty|Eliza"); RWCString s("Betty II is the Queen of England."); s.replace(re, "Elizabeth"); cout << s << endl; s = "Leg Leg Hurrah!"; re = "Leg"; s.replace(re, "Hip", RWCString::all); cout << s << endl; }
Program Output:
Elizabeth II is the Queen of England. Hip Hip Hurrah!
Note that the function call operator for RWCString has been overloaded to take an argument of type RWCRExpr. It returns an RWCSubString matching the expression, or the null substring if there is no such expression.