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.
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.
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.
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.
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.
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.
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.