Regex2 - Character classes

01st May, 2009 | regex

These indicate which characters will match a character at a single point. For example this will match 'cat', 'bat' and 'sat'

egrep '[cbs]at' file.txt

Within a character class a dash is a metacharacter (unless it's at the beginning, where it wouldn't make sense). When a dash isn't in a character class it is a literal. Beware!

This matches a valid 4bit hexadecimal value.:

egrep '0x[0-9A-F]' file.txt

Negated character classes are described '[^]' and it matches any character that isn't listed. This example allows 'dog' '5og' etc but not 'bog':

egrep '[^b]og' file.txt