Lab 8: Regex in a Spreadsheet
Due Sunday, October 25, 11:59 PM MT. One workbook, two tabs, one upload.
A lot of the text an accountant works with was typed by a person, for a person. This lab turns two kinds of it into columns you can sort, filter and join. Tab 1 uses regular expressions. Tab 2 uses an AI chat in the spreadsheet, and then your own check. Tab 2 goes straight into Project 2.
1. What you turn in
- The finished workbook as .xlsx, both tabs, with your REGEXEXTRACT formulas left in the Tab 1 columns.
- The closing questions.
Learning objectives
- Spot text patterns that a regular expression can pull out.
- Extract fields with REGEXEXTRACT in Google Sheets or Excel.
- Use an AI chat in a spreadsheet for a first pass, then check every row it returns.
2. The data
Download lab_08_workbook.xlsx. It has two tabs.
orders has the structured columns id, date, customer_id, upc, number_items and total, and one unstructured column, notes. A note usually looks like this:
Inv #: INV2022-68987
Rep: Gimli Gloinsson
Follow-up By: 2022-07-07
Notes: Inspected the Dwarven axe heads delivery. Minor chipping on three units (Lot AX-77). Initiated RMA process. Billing adjustment pending.
The notes have inconsistencies, typos and missing fields. That is normal for real data.
- Every field has a title followed by a colon. You can search for each title, or write a pattern for the general shape that ends at the colon.
- Invoice: the title can be Invoice #, Invoice#, Inv # or Inv#. Logic: Inv, then anything up to #:, then the rest of the line.
- Account rep: the title can be Rep, Representative, Account Rep or ARep. Logic: an optional A-word, then Rep, then anything up to the colon, then the rest of the line.
- Follow-up date: it can be missing. The title can be Follow-up Date, Follow up Date, Follow-up On, Follow-up By or Follow-up. Logic: Follow, then anything up to the colon, then the rest of the line.
- Notes: they can spill onto the next lines. The title can be Note or Notes. Logic: Note, an optional s, the colon, then everything to the end.
The four empty columns on the right, Invoice_Number, Contact_Name, Follow_Up_Date and Clean_Notes, are where your results go. Keep those names, because they are what my code looks for. Yes, the irony of grading a lab about flexible text parsing with an inflexible algorithm is not lost on me.
states has one row for each state and country pair exactly as a Project 2 owner typed it. Section 4 covers it.
3. Tab 1: regular expressions
Option A: Google Sheets
- Upload the workbook to Google Drive and open it with Google Sheets.
- In H2, the first Invoice_Number cell, enter
=REGEXEXTRACT($G2, "(?:Inv\w*\s*#?):\s*([^\n]+)") and fill it down. The part in parentheses is what comes back.
- Repeat for Contact_Name, Follow_Up_Date and Clean_Notes with your own patterns. Use exactly one capturing group, the part you want back. Make any other group non-capturing with
(?:...).
- When a field is missing, REGEXEXTRACT returns #N/A. To leave the cell blank instead, wrap it:
=IFERROR(REGEXEXTRACT(...), "").
- A dot does not match a line break unless the pattern starts with
(?s). That matters for the notes.
- Download with File > Download > Microsoft Excel (.xlsx).
If you get stuck on a pattern, ask Gemini in Sheets or any AI chat to draft one, then have it explain the pattern piece by piece. The formula is what you turn in, so you should be able to read it.
Option B: Excel
- Excel for Microsoft 365 has the same REGEXEXTRACT function. Excel returns the whole match by default, so add 2 as the third argument to get the part in parentheses:
=REGEXEXTRACT($G2, "(?:Inv\w*\s*#?):\s*([^\n]+)", 2).
- Fill it down and repeat for the other three columns. IFERROR and
(?s) work the same way.
- If your Excel has no REGEXEXTRACT, use Google Sheets.
Leave the formulas in place when you download. On Tab 1 the formula is the deliverable: it is the repeatable step. Typed or pasted values earn half credit.
4. Tab 2: states, with AI in the spreadsheet
Each row is a state and a country exactly as a Project 2 owner typed it at signup. Project 2 needs each one mapped to the state it reports to, as a two-letter code. That code is how the owners join to your Schedule A table in the project, so this tab is a piece of Project 2. Keep your checked copy.
The rule, from the Project 2 counsel memo:
- Keep state_raw and country_raw unchanged, including blanks. The two together identify a row. Fill state_code for every row, including the fully blank one in row 2.
- Before you decide, ignore capitals and outer spaces, and drop one period at the end. So AZ, az , AZ. and Arizona are all the same state.
- If the country is US or USA and the state is one of the fifty, by code or by full name, enter its two-letter code as state_code, for example AZ for Arizona.
- Write each state_code as two capital letters, with no spaces before or after and nothing else in the cell: AZ, not az, AZ. or " AZ ".
- If the country is foreign, or the state is missing or not one of the fifty, enter DE, for Delaware. That is the memo's rule for the incorporation state. It is a course rule, not legal advice.
AI can do the first pass, by chat. I will demo both of these in the live session.
- Google Sheets: open Gemini in Sheets. Give it the rule and the state_raw and country_raw columns, and ask for one state_code per row, in the same order.
- Excel: open the Copilot chat window. Give it the same rule and the same two columns, and ask for the same thing.
Put the whole rule in the prompt. Paste the answers into the state_code column as plain values, and check that every row got one. Then check every row yourself against the rule. The AI's answer is a draft. On Tab 2 the checked answer is the deliverable. No formula is needed, and pasted final answers earn full credit.
If your account has neither, paste the two input columns into any AI chat, paste the answers back, and check them the same way. A plain lookup works too: the fifty-state lookup has every name and code.
5. Submit in Canvas
- Upload the workbook as .xlsx, named <uID>_lab_08.xlsx, replacing <uID> with your university ID.
- Answer the closing questions, then submit the quiz. Check that Canvas shows your submission.
6. Points
- The workbook: 98.
- Tab 1, orders: 50, or 12.5 for each of the four columns. Each column is compared with the expected values, the same way for every student. Full credit needs your REGEXEXTRACT formula in the column. Typed or pasted values earn half.
- Tab 2, states: 48, in proportion to the rows whose state_code follows the rule. No formula is needed.
- The closing questions: 2, for 100.