Matching Groups of Characters

So far, so good? The regular expressions we've been building so far shouldn't strike you as being that complex, particularly if you look at each pattern in the way Perl does, character by character and alternate by alternate, taking grouping into effect. Now we're going to start looking at some of the shortcuts that regular expressions provide for describing and grouping various kinds of characters.

Character Classes

Say you had a string, and you wanted to match one of five words in that string: pet, get, met, set, and bet. You could do this:


That would work. Perl would search through the whole string for pet, then search through the whole string for get, then do the same thing for met, and ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.