Character class abbreviations allow you to match any of a set of characters without too much hassle. One way to do this is to put the set of characters you want to match from within []. For instance [0123456789] would allow you to match any of those numbers. This can be kind of cumbersome. You can also negate a character class by placing a caret at the front of it. For instance [^0123456789] matches anything that is not a number. You shouldn't be surprised that Perl makes your life much easier by defining some character class a bbreviations. These are alphanumeric characters preceded by a backslash. Perl allows you to match any number with a \d in your regular expression.

Now for a quick word about metacharacters. Metacharacters are characters that have special meaning within regular expressions. Therefore if you put them into a regular expression they won't match literally. Unless you precede the metacharacter with a \. The metacharacters are \|()$^.?* Now for a quick word about each of them do before we return to character class abbreviations.
.Matches any character besides newline
()Used for grouping characters
[]Used for defining character classes
|Used for or in regular expression
\Denotes the beginning of a character class abbreviation, or for the following metacharacter to be matched literally
*Quantifier matches 0 or more of the previous character or group of characters
?Makes a quantifier nongreedy
^Matches the beginning of a string (or line if /m is used)
$Matches the end of a string (or line if /m is used)

Now lets define some character classes

Character ClassMeaning
\ddigit or [0123456789]
\Dnondigit or [^0123456789]
\wword (alphanumeric) or [a-zA-Z_0-9]
\bword boundary
\swhitespace character [ \t\r\n\f]
\Snon whitespace character

That's a lot of information to get a handle on. So lets check out pattern-matching examples

In reply to Character Class Abbreviations by root

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.