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

Learning objectives

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.

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

  1. Upload the workbook to Google Drive and open it with Google Sheets.
  2. 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.
  3. 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 (?:...).
  4. When a field is missing, REGEXEXTRACT returns #N/A. To leave the cell blank instead, wrap it: =IFERROR(REGEXEXTRACT(...), "").
  5. A dot does not match a line break unless the pattern starts with (?s). That matters for the notes.
  6. 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

  1. 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).
  2. Fill it down and repeat for the other three columns. IFERROR and (?s) work the same way.
  3. 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:

AI can do the first pass, by chat. I will demo both of these in the live session.

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

  1. Upload the workbook as .xlsx, named <uID>_lab_08.xlsx, replacing <uID> with your university ID.
  2. Answer the closing questions, then submit the quiz. Check that Canvas shows your submission.

6. Points