Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Tools Module User's Guide

4.6 Pattern Matching

Class RWCString supports a convenient interface for string searches. In the example below, the code fragment:

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:

which will result in i set to 14. You can make a case-insensitive comparison with:

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.

4.6.1 Regular Expressions

As part of its pattern-matching capability, the Essential Tools Module supports simple and extended regular expression searches through its new RWTRegex<T> interface. Using RWTRegex<T> gives you access to wchar_t support, {m,n} cardinality constraint ability, and improved performance.

Extended regular expressions are the regular expressions used in the UNIX utilities lex and awk. Extended regular expressions can be any length, although limited by available memory. You will find details of the regular expression syntax in the Essential Tools Module Reference Guide under RWTRegex<T>. (Also, see the Bibliography.)

RWTRegex<T> is based on the POSIX.2 standard for regular expressions. POSIX.2 includes notations for basic regular expressions (BREs) and extended regular expressions (EREs). RWTRegex<T> is based on the ERE standard to support the wide character searches required by many non-Latin languages.

NOTE -- If your regular expression search requires the usage of backreferences, you will need to use RWCRegexp, rather than RWTRegex<T>.

In order to offer backwards-compatibility with RWCRegexp and RWCRexpr regular expression syntax, the RWTRegex<T>-supported syntax differs slightly from the POSIX standard. For detail, see the Essential Tools Module Reference Guide under RWTRegex<T>. The RWTRegex<T> Interface

RWTRegex<T> can perform both simple and extended regular expressions. The interface includes four primary classes:

The two regular expression classes in previous releases -- RWCRegexp and RWCRExpr -- are now deprecated. However, if your program requires the use of backreferences, you must use RWCRegexp, as the newer RWTRegex<T> does not include that functionality. (Backreferences allow you to match new text with previously-matched text in a regular expression, even if you don't know the text itself. Using Regular Expressions Efficiently

The results of performance tests show that the new RWTRegex<T> interface performs matching operations approximately seven to eight times faster than did the previous interface. Compiling the original pattern is, however, slightly slower.

To maximize efficiency when pattern matching, first instantiate RWTRegex<T> to compile the pattern once. Then perform any repeated matching operations. Introductory Examples Using Regular Expressions

You can use a regular expression to return a substring; for example, here's how you might match all Windows messages (prefix WM_):

Program Output:

The search method determines if there is a match. The what method obtains a matched string.The following example shows some of the capabilities of extended regular expressions:

Program Output:

Previous fileTop of DocumentContentsIndex pageNext file

©2004 Copyright Quovadx, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.