Understanding Basic Regular Expressions Patterns
As promised, the following table contains the most commonly used metacharacters.
Table 1: Commonly used regular expressions metacharacters
Expression | Description |
. | Matches any character except \n |
[characters] | Matches a single character in the list |
[^characters] | Matches a single character not in the list |
[charX-charY] | Matches a single character in the specified range |
\w | Matches a word character, same as [a-zA-Z_0-9] |
\W | Matches a non-word character |
\s | Matches a whitespace character; same as [\n\r\t\f] |
\S | Matches a non-whitespace character |
\d | Matches a decimal digit; same as [0-9] |
\D | Matches a nondigit character |
^ | Match the beginning of a line |
$ | Match the end of a line |
\b | On a word boundary |
\B | Not on a word boundary |
* | Zero or more matches |
+ | One or more matches |
? | Zero or one match |
{n} | Exactly n matches |
{n,} | At least n matches |
{n,m} | At least n but no more than m matches |
( ) | Capture matched substring |
(? | Capture matched substring into group name |
| | Logical OR |
More Advanced Uses of Regular Expressions
At this point, you have the basic knowledge required to form regular expressions and use them in your Managed C++ code. While what you've learned thus far will work for a lot of common parsing needs, regular expressions allow you to do so much more than search for simple character patterns. For example, you can:- Search for email addresses where the number of valid formats leads to very complex patterns
- Search and replace specific patterns
- Extract specific information, such as searching for phone numbers and then extracting only the area code
Download the Code
To download the code for the demo application, click here.
About the Author
Tom Archer owns his own training company, Archer Consulting Group, which specializes in educating and mentoring .NET programmers and providing project management consulting. If you would like to find out how the Archer Consulting Group can help you reduce development costs, get your software to market faster, and increase product revenue, contact Tom through his Web site.
Page 2 of 2