1. Computing

Reference: Named Character Classes

Handy Shortcuts for Ruby

By

Named character classes are handy shortcuts for commonly used classes of characters. Good examples are the \s for whitespace and \d for the digits zero through nine. Ruby also has more verbose versions that can make regular expressions more readable, such as [:digit:] for digits instead of \d.

  • Numerical Digits - \d or [:digit:]

    Any number 0-9.

  • Letters - [:alpha:]

    Any letter of the alphabet, either in capital or lowercase form.

  • Alphanumeric Characters - [:alnum:]

    Any number 0-9 or letter a-z in either capital or lowercase form. Follows locales, so language-specific letters will be matched.

  • Lowercase Letters - [:lower:]

    Any lowercase letter.

  • Uppercase Letters - [:upper:]

    Any uppercase letter.

  • Whitespace - [:blank:] or \s

    Any character that doesn't print anything. This includes spaces, tabs, line feeds and form feeds.

  • Punctuation - [:punct:]

    Punctuation characters such as exclamation points, periods, commas and question marks.

  • Word Characters - \w

    Any character that could possibly make a word including letters (uppercase and lowercase), numbers and _, the underscore character.

  • Not Word Characters - \W

    Note the upper-case W. Any character that's not a word character covered by \w.

  • Hexadecimal Characters - [:xdigit:]

    Any character that could be part of a hexadecimal number. This includes the numbers zero through nine, as well as the letters a through f in both lowercase and uppercase form.

  1. About.com
  2. Computing
  3. Ruby
  4. Regular Expressions
  5. Reference: Named Character Classes

©2014 About.com. All rights reserved.