Sub topic 12
Putting it together: cleaning a column of messy data
This chapter combines SPLIT, JOIN, REGEXEXTRACT, IFERROR, VALUE, TRIM, FILTER and ARRAYFORMULA to clean a column of messy data. It assumes you already know how each function works on its own: SPLIT's delimiter options, how results spill into neighboring cells, JOIN's delimiter-first argument order, and the basics of RE2 patterns and capture groups. Here the focus is on how the functions fit together and where the combinations break.
The sample data
Imagine an export from a small order system. Each row has three messy fields.
| Row | A: tags | B: note | C: contact |
|---|---|---|---|
| 2 | ` red; blue ;;green ` | `Order ORD-10492 shipped` | `Mail Ana at [email protected] today` |
| 3 | `blue;red` | `ord-7 pending` | `no email given` |
| 4 | (blank) | `ORD-300 returned, refund 45 USD` | `[email protected]` |
The goal is three clean outputs:
- Tags: one tidy string such as
red, blue, green. - Order ID: separate prefix and number columns, with the number stored as a real number.
- Email: a lowercase address, or an empty cell when none exists.
The same pipeline shape applies to each field: break the text apart, clean the pieces, put them back together, and decide what happens when a row does not fit.
Cleaning a delimited field: split, trim, rejoin
Start with the tags in A2. Splitting on the semicolon handles the delimiter, and the default remove_empty_text of TRUE drops the empty piece produced by ;;. The pieces still carry stray spaces, so trim each one:
=JOIN(", ", ARRAYFORMULA(TRIM(SPLIT(A2, ";"))))For " red; blue ;;green " the result is red, blue, green.
The ARRAYFORMULA wrapper matters. TRIM normally takes one value, and without ARRAYFORMULA it would act on only the first piece of the spilled row. Inside ARRAYFORMULA, TRIM runs over every piece, and JOIN receives a plain array.
For A4, which is blank, SPLIT returns an empty result and JOIN returns an empty string, so a blank input stays blank without extra handling.
Why this formula does not fill down as an array
A common next step is to widen the formula to the whole column:
=JOIN(", ", ARRAYFORMULA(TRIM(SPLIT(A2:A4, ";"))))This produces one long string for all three rows combined. SPLIT returns a two-dimensional block (one row per input row), and JOIN flattens any range or array into a single string. JOIN has no per-row mode.
There are two ways around this.
- Put the single-cell formula in each row and fill it down. This is simple and easy to debug.
- Use BYROW with LAMBDA to run the single-row logic once per row, with one formula for the whole column:
=BYROW(A2:A4, LAMBDA(r, JOIN(", ", ARRAYFORMULA(TRIM(SPLIT(r, ";"))))))The second form returns three results, one per row. Both approaches give the same output, so choose by how you maintain the sheet. A single spilling formula cannot get out of sync when rows are added, but a blocked spill range affects the whole column at once.
Cleaning a pattern field: extract, guard, convert
The notes in column B contain order IDs of the form letters, a hyphen, then digits. A first attempt:
=REGEXEXTRACT(B2, "([A-Z]+)-(\d+)")Row 2 returns ORD and 10492 in two adjacent cells. Row 3 returns #N/A, because ord-7 is lowercase and RE2 matching is case-sensitive by default.
Make the pattern tolerant
Add the (?i) flag at the start of the pattern to ignore case:
=REGEXEXTRACT(B3, "(?i)([a-z]+)-(\d+)")This now returns ord and 7. If you want consistent output, wrap the first group in UPPER. That requires splitting the extraction from the formatting, which is covered under the number column below.
Guard rows with no match
A row such as no order has no ID, and REGEXEXTRACT then returns #N/A. Wrap the call in IFERROR and return an empty string:
=IFERROR(REGEXEXTRACT(B2, "(?i)([a-z]+)-(\d+)"), "")When the extraction returns two values, IFERROR replaces the error in each output cell separately.
Run it down the whole column
REGEXEXTRACT accepts a range when wrapped in ARRAYFORMULA, and multiple capture groups produce multiple columns:
=ARRAYFORMULA(IFERROR(REGEXEXTRACT(B2:B4, "(?i)([a-z]+)-(\d+)"), ""))For the sample data, the output is:
| Row | Prefix | Number (text) |
|---|---|---|
| 2 | ORD | 10492 |
| 3 | ord | 7 |
| 4 | ORD | 300 |
The second column is text, which you can tell because it left-aligns by default and SUM ignores it. For a column you can calculate with, extract only the digits and convert them:
=ARRAYFORMULA(IFERROR(VALUE(REGEXEXTRACT(B2:B4, "(?i)[a-z]+-(\d+)")), ""))Because the pattern has exactly one capture group, REGEXEXTRACT returns only the digits, and VALUE turns them into numbers. Row 4 has two number-like strings: 300 in the ID and 45 in the refund text. Anchoring the digits to the hyphen prefix selects the right one. The simpler "\d+" would also work on row 4 because it returns the first match, but it would break on a note like Refund 45 USD for ORD-300.
The (?i) flag is also why the result is ORD in two rows and ord in another. To normalize the prefix, apply UPPER outside the extraction:
=ARRAYFORMULA(IFERROR(UPPER(REGEXEXTRACT(B2:B4, "(?i)([a-z]+)-\d+")), ""))Leading zeros disappear
VALUE converts "007" to 7. If the ID must keep its zeros, leave it as text. A good rule is to convert only the fields you will sum, average, sort numerically, or compare.
Extracting and normalizing emails
Emails are case-insensitive in practice, so lowercase them after extraction. The first match in each cell is returned, and rows without an address become blank:
=ARRAYFORMULA(IFERROR(LOWER(REGEXEXTRACT(C2:C4, "[\w.+-]+@[\w-]+\.[\w.]+")), ""))For the sample data:
- Row 2 returns
[email protected]. The surrounding wordsMail Ana atandtodayare not part of the match. - Row 3 returns an empty string.
- Row 4 returns
[email protected]. The+is allowed because it appears in the first character class.
This pattern is a practical filter, not a full email validator. It accepts some strings that are not valid addresses, and it can match a trailing period if the sentence ends right after the address, because . is allowed in the last character class. Run a spot check on real data before relying on it.
Joining the cleaned pieces and skipping blanks
Now suppose the cleaned values sit in separate cells, D2 for prefix, E2 for number, and F2 for email, and you want a single summary string. JOIN accepts a range, so FILTER can remove the blanks first:
=IFERROR(JOIN(" | ", FILTER(D2:F2, D2:F2<>"")), "")Without FILTER, a blank cell leaves a doubled separator, such as ORD | 300 | . With FILTER, only non-empty values remain.
The outer IFERROR is needed for a different reason. If all three cells are blank, FILTER finds no matching values and returns #N/A. The wrapper turns that case into an empty string.
A combined recipe to adapt
Many real cleanups chain the same ideas. Here is a formula that takes a semicolon-separated list of mixed-case emails and returns only the valid-looking addresses, lowercased and comma-separated:
=IFERROR(JOIN(", ", FILTER(ARRAYFORMULA(LOWER(TRIM(SPLIT(A2, ";")))), REGEXMATCH(ARRAYFORMULA(LOWER(TRIM(SPLIT(A2, ";")))), "^[\w.+-]+@[\w-]+\.[\w.]+$"))), "")This repeats the SPLIT expression, which is hard to read. It is a good candidate for LET if your version of Sheets supports it, or for a helper column. Readability usually matters more than having everything in one cell.
Troubleshooting checklist
When a combined formula returns something unexpected, check these in order:
- Evaluate each stage alone. Put the SPLIT in one cell, the TRIM in the next, and so on. Most failures are visible at one specific stage.
- Look for a blocked spill. A
#REF!error saying the array result was not expanded means a non-empty cell sits in the way. Clear the cells to the right or below. - Check for invisible whitespace. Non-breaking spaces (character 160) are not removed by TRIM. If
TRIMseems to do nothing, trySUBSTITUTE(A2, CHAR(160), " ")first. - Confirm the data type. If a comparison or sum fails, test with
ISNUMBERon the extracted value. - Remove IFERROR temporarily. The error message shows whether the pattern failed or the reference is wrong.
Exercise
Cell A2 contains Pending: 12 items; Shipped: 7 items;; Cancelled: 0 items. Write a single formula that returns the numbers of items as a comma-separated string, 12, 7, 0. Assume the number is always the only digit sequence in each part.
Show answer
Split on the semicolon, trim each piece, extract the digits from each piece, and join the results:
=JOIN(", ", ARRAYFORMULA(REGEXEXTRACT(TRIM(SPLIT(A2, ";")), "\d+")))
SPLIT drops the empty piece created by ;; because removeemptytext defaults to TRUE. REGEXEXTRACT inside ARRAYFORMULA runs on each of the three pieces. JOIN then puts the three results together with a comma and a space. The output is text; wrap each extraction in VALUE if you need numbers.
Exercise
You run =JOIN(", ", ARRAYFORMULA(TRIM(SPLIT(A2:A4, ";")))) and get one long string rather than three. Why does this happen, and what is one fix?
Show answer
SPLIT returns a two-dimensional block (one row per input row), and JOIN flattens the entire block into one string, so it cannot work row by row. Fix it by writing the single-cell formula in each row and filling down, or by using BYROW with LAMBDA: =BYROW(A2:A4, LAMBDA(r, JOIN(", ", ARRAYFORMULA(TRIM(SPLIT(r, ";"))))))
Exercise
A formula =ARRAYFORMULA(IFERROR(REGEXEXTRACT(B2:B100, "(\d+) units"), "")) returns blanks for every row, even rows that clearly contain text like "25 units". Name two things to check.
Show answer
First, remove IFERROR so the actual error is visible. The pattern may not match because of a typo, different spacing (for example a non-breaking space between the number and "units"), or different case such as "Units", which needs the (?i) flag. Second, check that the cell contents are really text in the form you expect, for example by displaying a sample cell with LEN or CODE to find hidden characters.