December 19, 2014
Hot Topics:

Understanding Basic Regular Expressions Patterns

  • March 1, 2005
  • By Tom Archer
  • Send Email »
  • More Articles »

As promised, the following table contains the most commonly used metacharacters.

  Table 1: Commonly used regular expressions metacharacters

ExpressionDescription
.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
\wMatches a word character, same as [a-zA-Z_0-9]
\WMatches a non-word character
\sMatches a whitespace character; same as [\n\r\t\f]
\SMatches a non-whitespace character
\dMatches a decimal digit; same as [0-9]
\DMatches a nondigit character
^Match the beginning of a line
$Match the end of a line
\bOn a word boundary
\BNot 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

Simply combine these metacharacters with what you learned in the previous articles on string splitting and using the Match and MatchCollection classes and you'll be surprised at how easily you can search for many basic patterns.

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
In order to move into these more advanced areas of regular expressions use, you'll need to know about groups and captures. Therefore, future articles will cover these areas and examine some of the tasks mentioned in this article.

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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel