Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Regex Quick Reference Card cheat sheet - grade 9-12

Click image to open full size

Computer Science Grade 9-12

Regex Quick Reference Card Cheat Sheet

A printable reference covering regex literals, character classes, quantifiers, anchors, groups, alternation, and escaping for grades 9-12.

Download PNG

Study as Flashcards

Regular expressions, often called regex, are patterns used to find, match, and validate text. This cheat sheet helps students read and write common regex patterns without memorizing every symbol. It is useful for programming, data cleaning, form validation, and searching through files.

Students in grades 9 through 12 can use it as a quick reference while coding or debugging.

Key Facts

  • The dot . matches any single character except a newline in most regex engines.
  • A character class like [abc] matches one character that is a, b, or c.
  • A negated character class like [^0-9] matches one character that is not a digit from 0 to 9.
  • The quantifier * means zero or more, + means one or more, and ? means zero or one of the previous token.
  • The quantifier {n} means exactly n times, {n,} means at least n times, and {n,m} means from n to m times.
  • The anchor ^ matches the start of a line or string, and the anchor $ matches the end of a line or string.
  • Parentheses create a group, so (cat|dog) matches either cat or dog as a complete choice.
  • A backslash escapes special characters, so \. matches a literal period instead of any character.

Vocabulary

Regular expression
A regular expression is a text pattern used to search, match, replace, or validate strings.
Literal
A literal is a character in a regex that matches itself, such as a matching the letter a.
Character class
A character class is a bracketed set of allowed characters, such as [A-Z] for one uppercase letter.
Quantifier
A quantifier tells how many times the previous character, class, or group may repeat.
Anchor
An anchor matches a position in text, such as the start or end, rather than matching a character.
Group
A group uses parentheses to treat part of a regex as one unit for repetition, alternation, or capture.

Common Mistakes to Avoid

  • Forgetting to escape special characters is wrong because symbols like ., ?, +, and * have regex meanings unless written with a backslash.
  • Using . when a specific character class is needed is wrong because . can match almost any character, making the pattern too broad.
  • Placing ^ inside brackets by accident is wrong because [^abc] means not a, b, or c, while ^ outside brackets usually means start of text.
  • Writing greedy patterns like .* for structured data is risky because it can match more text than intended before stopping.
  • Forgetting anchors is wrong when validating a whole string because [0-9]+ can match part of abc123, but ^[0-9]+$ requires the entire string to be digits.

Practice Questions

  1. 1 Write a regex that matches a three-digit number such as 482 but not 48 or 4821.
  2. 2 Which strings match ^[A-Z][a-z]+$: Sam, sally, A, Maria, JONES?
  3. 3 Write a regex that matches a simple date in the format 2026-05-23, using exactly four digits, a dash, two digits, a dash, and two digits.
  4. 4 Explain why ^[0-9]+$ is better than [0-9]+ when checking whether a whole input string contains only digits.

Understanding Regex Quick Reference Card

A regex engine usually searches by trying the pattern at one position in the text, then moving forward until it finds a match. This matters because finding a match is not the same as checking that an entire input is valid. A pattern for three digits may find 123 inside a longer string such as ID123X.

For validation, the pattern must require the beginning and end of the input. Students often get confused when a pattern works in a search box but accepts extra unwanted text in a form.

Groups do more than organize a pattern. They control which pieces belong together before a quantifier or an either choice is applied. For example, a repeated group can match a whole word unit several times, rather than repeating only its final letter.

Many programming languages store the text matched by groups. This is called capturing.

Captures are useful when a program needs to pull apart a date, filename, or name into separate pieces. Alternation is read from left to right in many engines, so placing a shorter choice before a longer overlapping choice can produce an unexpected result.

Quantifiers are often greedy by default. Greedy means they take as much text as possible while still allowing the rest of the pattern to match. A pattern using dot-star between two quotation marks can stretch from the first quotation mark to the last one on a line.

A lazy quantifier takes as little as possible at first. Neither choice is automatically better. The right choice depends on the structure of the text.

Broad patterns can force the engine to retry many possibilities. On very long text, nested repeating groups can become slow. Clear, specific patterns are easier to read and usually safer.

Regex appears in code editors, spreadsheet tools, command line searches, web forms, and data cleanup scripts. A student might use it to find repeated spaces, identify lines with a certain file extension, or check the basic shape of a school ID. It should not be treated as proof that information is real.

A regex can check that an email address has a plausible format, but it cannot prove that the mailbox exists. Test every pattern with examples that should match, examples that should fail, empty text, spaces, punctuation, and unusually long input. Different languages use slightly different regex rules, so students should check the documentation for the language or tool they are using.