Sub topic 11
Turning Extracted Text into Numbers
Assume you can enter formulas, reference cells and ranges, and use REGEXEXTRACT and IFERROR. This chapter focuses on one important detail: REGEXEXTRACT returns text, even when the matched characters look like a number.
Why extracted digits are still text
Suppose A2 contains:
ORD-10492This formula extracts the digits:
=REGEXEXTRACT(A2, "\d+")The result displayed in the cell is 10492, but Sheets stores it as text. Text that looks like a number cannot always be used reliably in arithmetic, sorting, or numeric comparisons.
Use VALUE to convert the extracted text:
=VALUE(REGEXEXTRACT(A2, "\d+"))The result is the number 10492. You can now use it in calculations:
=VALUE(REGEXEXTRACT(A2, "\d+")) + 1This returns 10493.
VALUE parses text that represents a number. It follows your spreadsheet's locale settings, so decimal marks and thousands separators may be interpreted differently in different locales.
A conversion workflow
The usual sequence is:
- Find the part you need with
REGEXEXTRACT. - Convert that part with
VALUE. - Use the result in arithmetic, comparisons, or other numeric functions.
For example, suppose A2 contains:
Invoice total: $1,275.50This formula extracts the digits and punctuation:
=REGEXEXTRACT(A2, "\d[\d,]*\.?\d*")The extracted result is the text 1,275.50. In a locale that accepts this number format, convert it as follows:
=VALUE(REGEXEXTRACT(A2, "\d[\d,]*\.?\d*"))If your locale does not interpret commas as thousands separators, remove them before conversion:
=VALUE(SUBSTITUTE(REGEXEXTRACT(A2, "\d[\d,]*\.?\d*"), ",", ""))SUBSTITUTE changes the comma-separated text into 1275.50, and VALUE converts it to a number.
| Input in A2 | Extraction formula | Converted result |
|---|---|---|
| `ORD-10492` | `=VALUE(REGEXEXTRACT(A2, "\d+"))` | `10492` |
| `Qty: 18` | `=VALUE(REGEXEXTRACT(A2, "\d+"))` | `18` |
| `Price: $1,275.50` | `=VALUE(SUBSTITUTE(REGEXEXTRACT(A2, "\d[\d,]*\.?\d*"), ",", ""))` | `1275.5` |
Handling rows without a match
REGEXEXTRACT returns #N/A when the pattern is not found. Wrap the complete conversion in IFERROR when some rows may be empty or have a different format:
=IFERROR(VALUE(REGEXEXTRACT(A2, "\d+")), "")This returns a blank when A2 contains no digits. You can return zero instead:
=IFERROR(VALUE(REGEXEXTRACT(A2, "\d+")), 0)Choose the fallback based on meaning. A blank says “no value was found.” A zero says “the numeric value is zero.” Those are different data conditions.
Capture groups and numeric parts
A pattern with capture groups can return several text values. For ORD-10492:
=REGEXEXTRACT(A2, "([A-Z]+)-(\d+)")The result spills into two cells: ORD and 10492. Convert the second result in the next cell:
=VALUE(B2)If the formula is in B2, the numeric capture group is in C2, so use:
=VALUE(C2)The conversion must target the cell containing the extracted digits, not the cell containing the whole spilled result.
How the values change type
Exercise
A2 contains Ticket 007. Write a formula that returns the number 7, and returns a blank when no digits are present.
Show answer
=IFERROR(VALUE(REGEXEXTRACT(A2, "\d+")), "")