Regex Patterns and Character Classes
\d, \w, \s, ., ^, $, +, *, ?, {n,m}, character classes [a-z], negation [^...].
Regex Patterns and Character Classes is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Regex Matters for Text AI
Text AI starts with messy raw text. Regular expressions (regex) are a compact language for finding and extracting patterns — phone numbers, dates, hashtags, URLs — so you can clean and structure text before feeding a model.
This course teaches patterns, the re module, groups, and a real text-cleaning pipeline.
Literal Characters
The simplest pattern is literal text: it matches exactly those characters. Regex is case-sensitive by default.
cat matches "cat" inside "the cat sat" but not "Cat".
import re
print(re.findall("cat", "the cat sat on a cat mat"))
# ["cat", "cat"]The Dot Metacharacter
The dot . matches any single character except a newline. It is a wildcard.
c.t matches "cat", "cot", "c9t" — anything with c, one char, then t.
import re
print(re.findall("c.t", "cat cot cut c@t"))
# ["cat", "cot", "cut", "c@t"]Digit, Word, and Space Classes
Shorthand character classes cover common groups:
\d— a digit (0-9)\w— a word character (letter, digit, or underscore)\s— whitespace (space, tab, newline)
import re
print(re.findall("\d", "a1b2c3")) # ["1", "2", "3"]
print(re.findall("\w", "a_1!")) # ["a", "_", "1"]
print(re.findall("\s", "a b\tc")) # [" ", "\t"]Negated Classes
Uppercase versions negate the class:
\D— anything that is NOT a digit\W— NOT a word character\S— NOT whitespace
import re
print(re.findall("\D", "a1b2")) # ["a", "b"]
print(re.findall("\S", "a b c")) # ["a", "b", "c"]Custom Character Sets with [ ]
Square brackets define your own set: match any one character listed inside. Ranges use a hyphen.
[aeiou]— any vowel[A-Za-z]— any letter[0-9]— any digit (same as\d)
import re
print(re.findall("[A-Za-z]", "ab12CD")) # letters only
print(re.findall("[aeiou]", "hello")) # ["e", "o"]Negated Sets with [^ ]
A caret inside the brackets negates the set: match any character NOT listed.
[^abc] matches anything except a, b, or c. (A caret outside brackets is an anchor, covered next.)
import re
print(re.findall("[^aeiou ]", "hello world"))
# consonants: ["h", "l", "l", "w", "r", "l", "d"]Anchors: ^ and $
Anchors match positions, not characters:
^— start of the string (or line)$— end of the string (or line)
^Hello matches only if the text begins with "Hello".
import re
print(bool(re.search("^Hello", "Hello world"))) # True
print(bool(re.search("world$", "Hello world"))) # True
print(bool(re.search("^world", "Hello world"))) # FalseQuantifiers: + * ?
Quantifiers say how many times the preceding item repeats:
+— one or more*— zero or more?— zero or one (optional)
import re
print(re.findall("\d+", "abc 123 de 45")) # ["123", "45"]
print(re.findall("ab*", "a ab abb")) # ["a", "ab", "abb"]
print(re.findall("colou?r", "color colour")) # both matchRange Quantifier {n,m}
Curly braces give an exact count or range:
{3}— exactly 3{2,4}— between 2 and 4{2,}— 2 or more
Great for fixed-length items like ZIP codes or phone segments.
import re
print(re.findall("\d{3}", "12 345 6789")) # ["345", "678"]
print(re.findall("\d{2,4}", "1 22 3333 55555"))Combining the Pieces
Real patterns combine classes, sets, anchors, and quantifiers. This one matches a simple US phone number like 555-123-4567:
\d{3}-\d{3}-\d{4} — three digits, dash, three digits, dash, four digits.
import re
text = "Call 555-123-4567 or 999-888-7777"
print(re.findall("\d{3}-\d{3}-\d{4}", text))
# ["555-123-4567", "999-888-7777"]Quick Check: Quantifiers
You want to match one or more consecutive digits.
Recap: Patterns and Character Classes
You now know the building blocks of regex:
- Literals and the
.wildcard - Classes
\d \w \sand their negations\D \W \S - Custom sets
[A-Za-z]and negated sets[^abc] - Anchors
^and$ - Quantifiers
+ * ?and ranges{n,m}
Next: the Python re module functions.
Frequently asked questions
Is the “Regex Patterns and Character Classes” lesson free?
Yes — the full text of “Regex Patterns and Character Classes” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Regex Patterns and Character Classes”?
\d, \w, \s, ., ^, $, +, *, ?, {n,m}, character classes [a-z], negation [^...]. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Regex Patterns and Character Classes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Regex Patterns and Character Classes
- re Module: search, match, findall, sub
- Capturing Groups and Named Groups
- Text Cleaning for AI with Regex