Regex Trainer

Built-in AI tutor. Stuck on a cell? Ask the helper on this page. It reads the code you ran and the checks you failed, points at the line, and asks questions. It does not write the answer.

A regular expression is a short pattern for the shape of valid text, such as "exactly five digits". The holder addresses behind Project 2 were typed by people, so the same state arrives as a code, a full name, with a trailing space or with a period, and postal codes come from the US, Canada and Mexico. A person reads past the mess. Python compares text exactly, so every field needs a rule. This page builds the pattern half of those rules in the same order as the live RegExr demonstration from class, with real Python running in your browser. It is optional practice, not graded, and nothing here submits to Canvas. The same pieces work inside REGEXEXTRACT in Google Sheets and Excel.

The scenarioSilicone Motors has to report unclaimed property to the right state. A ZIP code typed as Box 84101, rear, or a Mexican code that looks like a US ZIP, sends money to the wrong place. Before you clean a field, you decide what a valid value looks like. Then you test that decision against strings that must pass and strings that must fail.
Two questions decide the tool. Question 1: is the set of valid values known and finite, such as the 50 states, your chart of accounts or ISO country codes? Then use a lookup, a dictionary or mapping table. Question 2: is a valid value defined by its shape, such as a ZIP code, an invoice id or a date? Then use a regular expression. Many fields need both: clean the shape, then look the value up.
Lookup or regex. A closed list, like the states, is a lookup. A shape, like a ZIP code, is a pattern. Many fields need both. This page practices the pattern half.
The habit this page teachesWrite the test strings first: the ones that must pass and the ones that must fail. Passing them shows that those cases work. Add a few near misses of your own before you trust the pattern on real data.

Replay the live demonstration

Each link opens RegExr with the pattern and the test text from class already loaded. RegExr highlights every match as you type. Its Explain tool describes each token. Set the flavor menu to PCRE to stay close to Python.

  1. Step 0. Meet RegExr: Mac's month pattern
    It matches "Jan 2020" and also "All 2025". Why?
  2. Step 1. Five digits, anywhere
    A ZIP inside junk still matches.
  3. Step 2. Anchor the whole field
    Only lines that are exactly five digits.
  4. Step 3. An optional group: ZIP+4
    And 44100 still passes. Shape is not meaning.
  5. Your turn in class: a Canadian postal code
    Grow the starter pattern until each line is all or nothing.
  6. Step 4. Capture groups pull out the fields
    Reference, date and amount from bank memo lines.

The rest of this page is a notebook. Each code cell has an In [ ] on the left. Edit a line, press Shift+Enter or click Run, and the output appears underneath. Read each Predict card first and guess before you run, then open Check your prediction. Python loads in the background when the page opens. In any editor, press Esc and then Tab to move on with the keyboard.

0 · the data1 · a literal2 · digits and counts3 · the whole field4 · optional groups5 · sets and alternatives6 · capture groups7 · findall and \b8 · on purpose: errors9 · the lookup halfCheck yourself

What you just practiced

You built patterns the way analysts do: one piece at a time, against a fixed list of strings. \d and a count describe digits. re.fullmatch makes the whole field fit, where re.search finds a piece anywhere and re.findall finds every piece. A group with ? makes a part optional. A set or a list of alternatives decides which characters count, and \b decides where a match must stop. Named groups pull fields out of a line, and plain Python converts them. A dict lookup handles a closed list, and leaves anything unknown as None.

The one rule that trips people up: a pattern checks shape, not meaning. 44100 is a valid US ZIP shape and a valid Mexican postal code. No pattern can tell them apart. The country field can. When a value passes the pattern, it is well formed. It is not yet correct.
Three versions of a US ZIP pattern against eight test strings. Version A, re.search with five digits, accepts a ZIP inside junk and inside a cut-off code. Version B, re.fullmatch with five digits, rejects ZIP+4. Version C, re.fullmatch with an optional hyphen-four group, is right on every row except 44100, a Mexican code with a ZIP shape. Amber cells are wrong answers, computed by running Python.
The deck's test matrix. Every cell was computed with Python. The last wrong row is about country, which no shape can see.
AI prompt
Explain this Python regular expression one token at a time, then give five strings it should accept and five near-miss strings it should reject: \d{2}/\d{2}/\d{4}

Write your own test strings first. Then run the model's strings in the scratch cell and check every one. If you cannot explain a pattern, you don't own it.

Check yourself

Four small functions on invoice ids, credit memo ids, bank memo lines and amounts, like the ones in the live demonstration. They are practice formats, and nothing here is graded. Before you write a pattern, type two strings that must pass and two that must fail into the scratch cell and run them.

Check runs your function in a fresh Python namespace with re already imported, so it does not depend on the notebook cells above. Each function also needs a one-line docstring under its def. Some tests are hidden. A hidden test still shows what it checks, such as "lower case", and whether you passed it. A reload keeps your check marks but not your code, so copy any code you want to keep.

What the tutor sees, and a prompt to copy into another AI tool

The tutor reads the code in the cell or task you are working on, its last output and error, a short view of the other notebook cells, and your check results. It sees nothing you have not typed or run on this page. The preview shows the complete prompt, including earlier turns of the conversation.

Nothing on these pages submits to Canvas. Anything a page keeps stays in this browser.