Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

RegEx ReMarks

Some opinions, advice, and rants about regular expressions.

Regex Character Classes

I'm writing an article on regular expressions for msdn online and I've just finished a section on character classes.  Character classes, of course, are those sections of regular expression patterns enclosed in hard braces [ ].  One point I'm trying to express in the article which I think is a point of confusion for many people when dealing with regular expressions is that the grammar for character classes is wholly different from that of the rest of regular expressions.  They are basically a separate language within the language, with their own special syntax rules and metacharacters.  For instance, while $, ., and \ are metacharacters anywhere else in a regular expression, they are simply literals within character classes.  Likewise, although the hyphen - is nothing special in a regular expression, it has special meaning within character classes.

This is sometimes confusing, as you'll see people trying to create a ZIP+4 expression with an escaped hyphen because they think it's a metacharacter (wrong: \d{5}\-\d{4}   right: \d{5}-\d{4}).  Or trying to escape characters within a character class, like to match a $ or  period in a character class one might try this (wrong): [\$\.]  instead of this (right): [$.].

Another common misconception is that character classes can be used to match words, like expecting [img] to match the string 'img' in a pattern.  Sure it'll match it if you give it a quantifier ([img]{3}), but it would also match 'iii' or 'gmi' or any other 3-letter combination of those three characters.  Just remember, character classes only refer to a single character in the pattern -- you can't use them for words (well, not words that need to have a specific order and composition of letters, anyway).

Sponsor
Published Wednesday, March 03, 2004 1:12 PM by ssmith

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

TrackBack said:

March 4, 2004 8:51 AM
 

Regex Character Classes said:

November 27, 2007 4:12 AM

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below

Submit