Regular expressions in practice

The extended syntax of tools such as grep -E: dot, character classes, anchors, quantifiers, groups, alternation, escaping and greediness, with patterns tried on real lines.

  • 25 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The numbers
  5. Scenarios

A few cards from the deck

  • Character class: what is it?

    Show the answer

    A pattern in square brackets that stands for a single character among those listed: [abc] or [0-9]; with ^ right after the opening bracket, among all the others.

    [0-9] is one digit, [^0-9] is one character that is not a digit. A bare dot stands for any character.

  • What does the pattern <.*> capture on the following line?

    ```text
    <b>one</b> and <i>two</i>
    `

    • <b>one</b> and <i>two</i>
    • <b>
    • <b>one</b>
    • <i>two</i>
    Show the answer

    <b>one</b> and <i>two</i>

    The dot-star is greedy: it swallows everything up to the end of the line then backs up to the last >. Python (re.search) does return the whole line.

  • For the pattern to describe the entire line, place it between the anchors … at the start and … at the end.

    Show the answer

    For the pattern to describe the entire line, place it between the anchors ^ at the start and $ at the end.

    ^ and $ check a position, not a character. A form field is almost always validated with both.

  • Anchor: what is it?

    Show the answer

    A symbol that consumes no character but imposes a position: ^ for the start of the line, $ for its end.

    Without anchors, a pattern may match a piece of the line: [0-9]{5} finds five digits in 750115. With ^ and $, the whole line must match.