Python Arena
Step 2 of 4

Alternatives and character classes

A pattern of plain characters matches only that exact text. Special characters let one pattern match many different texts. This step shows the ones that choose between alternatives and between characters.

Alternatives

A vertical bar | means or: r"cat|dog" matches either cat or dog:

import re

for word in ["hotdog", "category", "parrot"]:
    print(word, re.search(r"cat|dog", word) is not None)
Output
hotdog True
category True
parrot False

Character classes

Square brackets make a character class, which matches one character: any of those listed. r"[aeiou]" matches one vowel. A dash between two characters gives a range: [0-9] is any digit, [a-z] any small letter and [A-Za-z] any letter. A dash first or last in the brackets has nothing to make a range with, so it's an ordinary dash: [a-z-] is a small letter or a dash. A ^ right after the opening bracket turns the class around: [^0-9] is any character that is not a digit. Classes in a row match characters in a row, so r"[0-2][0-9]" matches two digits, the first of them 0, 1 or 2:

In the Python interpreter
>>> import re
>>> re.findall(r"[aeiou]", "regular expression")
['e', 'u', 'a', 'e', 'e', 'i', 'o']
>>> re.findall(r"[A-Z]", "Ada Lovelace met Charles Babbage")
['A', 'L', 'C', 'B']
>>> re.findall(r"[^a-z ]", "it's 9 a.m.!")
["'", '9', '.', '.', '!']
>>> re.findall(r"[0-2][0-9]", "07 31 19 42 20")
['07', '19', '20']

Some classes are so common that they have a short form. \d is a digit, the same as [0-9]. \w is a "word character": a letter, a digit or the underscore _. \s is whitespace: a space, a tab or a line break.

The dot, the start and the end

A dot . matches any one character, except a line break. ^ matches at the start of the text and $ at its end, without using up any character. With them you can check how a text starts or ends, or, with both, the whole text:

import re

for code in ["A1", "B7x", "x9", "C3"]:
    if re.search(r"^[A-Z][0-9]quot;, code):
        print(code, "is a valid code")
print(re.findall(r"c.t", "cat, cot, coat, cut"))
Output
A1 is a valid code
C3 is a valid code
['cat', 'cot', 'cut']

Without the ^ and the $, B7x would be valid too, because B7 is in it. And coat isn't found by c.t, because the dot stands for exactly one character.

Special characters as themselves

To match one of the special characters . ^ $ * + ? ( ) [ ] { } | \ as an ordinary character, put a backslash before it: r"\." matches a dot and r"\quot; a dollar sign. Compare:

In the Python interpreter
>>> import re
>>> re.findall(r"\d.\d", "1.5 and 2x5")
['1.5', '2x5']
>>> re.findall(r"\d\.\d", "1.5 and 2x5")
['1.5']

Inside square brackets most special characters lose their meaning, so [.?!] matches a period, a question mark or an exclamation mark.

Your task

Challenge 1 of 3

Fill in the pattern so that the program finds the orders of tea or coffee. The program should print:

hot: green tea
hot: black coffee

Challenge 2

What does this program print? Type each line it prints.

Challenge 3

A square on a chess board is named by a letter from a to h and a number from 1 to 8, such as e4. Write a function is_chess_square(text) that returns True if the whole text is such a name, and False otherwise. Use a regular expression. For example,

print(is_chess_square("e4"))
print(is_chess_square("i9"))
print(is_chess_square("a10"))

should print:

True
False
False