Concepts / Character Classes and Square Brackets

Character Classes and Square Brackets

The central decision is whether a special character should keep its regular-expression meaning or be matched as the actual character visible in the text.

  • Programming

Why Literal Symbols Matter

Regular expressions use some characters as functional commands rather than as ordinary text. When the text you want to find contains one of those special characters, the backslash is the escape character: it tells the regular-expression engine to treat the following character as a literal symbol.

Key point

The central decision is whether a special character should keep its regular-expression meaning or be matched as the actual character visible in the text.

Tracing a Currency Match

In the real world

Suppose the text is: We just received $10.00 for cookies. The goal is to find the dollar amount. The pattern needs to recognize a literal dollar sign, followed by characters representing the amount.

\$literal dollar sign$currency symbol[0-9.]+digits or periods10digits.period00digitsmatchesmatchesmatchesmatches
How does the escaped dollar sign and character class align with the characters in the amount?

In this pattern, \$ matches the actual dollar sign. The backslash is essential because an unescaped dollar sign is interpreted as the end-of-line function rather than as the currency symbol. The [0-9.]+ portion matches one or more characters that are either digits from 0 through 9 or a period.

Character Sets in Brackets

Definition

Square brackets define a character class: a group of characters that can be matched as the allowed character set. In [0-9.]+, the class contains the digits 0 through 9 and the period character. The plus sign indicates that one or more characters from that set are matched in this pattern.

[starts the class0-9digits.period]ends the class+one or morecontainsshares class withinsidefollowed by
What characters are represented by the bracketed part of the currency pattern?

Inside and Outside Brackets

The same symbol can be treated differently depending on its location. Outside square brackets, special characters retain their regular-expression functions unless a backslash escapes them. Inside square brackets, characters are already treated literally and generally do not require escaping.

$functional meaning$literal symbol.functional meaning.literal symbol^functional meaning^literal symbolplaced inside bracketsplaced inside bracketsplaced inside brackets
How does a special symbol change when it moves from outside a character class to inside square brackets?
LocationHow special characters are treatedWhat to do when matching the symbol literally
Outside square bracketsThey can retain their functional regular-expression meaningsPrefix the special character with a backslash
Inside square bracketsCharacters are already treated literallyEscaping is generally not required

Use the character's location to decide whether an escape is needed.

The Escape Transformation

.special character\.escaped characterprefix with backslash
What changes when a backslash is placed before a special character outside square brackets?

Escaping changes the instruction given to the regular-expression engine. A special character outside square brackets can act as a functional command. Prefixing it with a backslash converts it into a literal match. The same principle applies to common special characters such as $, ., ^, +, *, ?, {, }, [, ], the backslash itself, and |.

Best practice

Escape a special character only when it appears outside square brackets and you want the actual symbol to be matched. Do not add escapes automatically to every symbol; first identify whether the symbol is inside a character class.

Mistakes with Escaping

  • Common mistake

    Leaving the dollar sign unescaped when looking for a currency amount.

    What it looks like

    Using $ where the text contains the literal symbol $10.00.

    Why it fails

    Outside square brackets, the dollar sign has a regular-expression function meaning the end of a line, so it does not represent the literal currency symbol.

    Do this instead

    Use \$ for the literal dollar sign.

  • Common mistake

    Assuming every character inside square brackets needs a backslash.

    What it looks like

    Adding escapes to characters that are already inside a character class.

    Why it fails

    Characters inside square brackets are already treated literally and generally do not require escaping.

    Do this instead

    Check the character's location before deciding whether to escape it.

  • Common mistake

    Reading the contents of a character class as one complete string.

    What it looks like

    Reading [0-9.] as the literal sequence 0-9. rather than as a set of permitted characters.

    Why it fails

    The bracketed portion describes characters that may be matched; in the currency pattern, [0-9.]+ matches one or more digits or periods.

    Do this instead

    Interpret the brackets as a character set and then interpret the following plus sign as allowing one or more matches.

Practice the Decision

EASY

A text pattern must find the literal dollar sign in a financial string. Decide whether the dollar sign should appear escaped when it is outside square brackets. Then explain why the period in the bracketed portion of [0-9.]+ does not need a separate escape for this use.

Hints
  • Ask whether each symbol is outside or inside square brackets.
  • Remember that the dollar sign has a functional meaning outside brackets.
  • Remember that characters inside a character class are already treated literally.

What do you think happens?

For the currency text We just received $10.00 for cookies., which part of the pattern is responsible for matching the literal dollar sign?

  • \$

  • [0-9.]+

  • +

Reveal answer
Answer:

\$

The backslash changes the dollar sign from its functional regular-expression meaning into a literal dollar-sign match. The bracketed portion handles the digits and period.

Key Takeaways

  1. 1
    The backslash is the escape character used to make a special regular-expression character literal.
  2. 2
    Outside square brackets, special characters may keep their functional meanings and need escaping when the literal symbol is required.
  3. 3
    Inside square brackets, characters are already treated literally and generally do not need escaping.
  4. 4
    The currency pattern uses \$ for the literal dollar sign and [0-9.]+ for one or more digits or periods.
  5. 5
    Always decide how to write a symbol by checking both its special meaning and its location in the pattern.

At a glance

  • Use the backslash to convert a special regular-expression character into a literal match.
  • Outside square brackets, escape special characters when their actual symbols must be found.
  • Inside square brackets, characters are already treated literally and generally do not need escaping.
  • In a currency pattern, \$ matches the dollar sign and [0-9.]+ matches one or more digits or periods.

Related concepts

More concepts starting with C