How to Extract Numbers from Text in Excel with Regex

Published on

You can use the REGEXEXTRACT or REGEXREPLACE functions to easily extract numbers from text in Excel. The choice of function depends on whether your text contains multiple numbers or just one.

Many applications import data into Excel, and when working with such data, you may encounter numbers embedded in text strings.

For example:

  • Invoice numbers such as “Invoice #1564”
  • Product IDs such as “AI123456”
  • Amounts such as “USD 1500.00”
  • Text strings such as “January sales are worth 10.5 million US dollars, and February sales are worth 12.5 million US dollars”

You might want to extract numbers from the left, right, a specific position, or all numbers from a text string in Excel. Depending on the scenario, you can use REGEXEXTRACT, REGEXREPLACE, or a combination of functions like REGEXREPLACE, TEXTSPLIT, and CHOOSECOLS.

Let’s look at examples for all these scenarios below.

Regex to Extract Numbers from Left of Text in Excel

Assume the text in cell A1 is “Invoice #1564”. The following REGEXEXTRACT formula will return 1564:

=VALUE(REGEXEXTRACT(A1, "\d+(\.\d+)?"))

If the text is “January sales are worth 10.5 million US dollars, and February sales are worth 12.5 million US dollars”, the output will be the first number in the string, which is 10.5.

Example of using Regex to Extract Numbers from the Left of Text in Excel

This formula can extract both decimal numbers and integers from the text. If there are multiple numbers, it will extract the first one.

Regarding the regex pattern, \d+ matches one or more digits, and (\.\d+)? optionally matches a decimal point followed by one or more digits. This ensures the formula extracts the first number, including decimals if present.

Regex to Extract Numbers from Right of Text in Excel

The following formula will extract the last number present in the text string in Excel:

=VALUE(REGEXEXTRACT(A1, "\d+(\.\d+)?(?=[^\d]*$)"))

If the text contains just one number, it will return that number. If there are multiple numbers, it will return the last number. Similar to the first example, this formula is also capable of extracting both decimals and integers.

Example of using Regex to Extract Numbers from the Right of Text in Excel

The first part of the regex pattern, \d+(\.\d+)?, is the same as in our first formula, used to extract the first number, including decimals if present. The last part, (?=[^\d]*$), is a positive lookahead that ensures the number being matched is followed by non-digit characters (or no characters at all), meaning it’s the last number in the string.

How to Extract All Numbers from Text in Excel

When you have multiple numbers in a string and want to extract all numbers from text in Excel into individual cells in a row, you can use the following combination formula:

=VALUE(TEXTSPLIT(REGEXREPLACE(REGEXREPLACE(A1, "[^\d.]+", "|"), "^\||\|$", ""), "|"))
Example of Extracting All Numbers from Text in Excel Using Regex

In this formula:

  • The inner regex replaces all non-numeric and non-period characters (one or more times, indicated by +) with a single pipe (|).
  • The outer regex replaces any leading or trailing pipe characters (|) with an empty string, i.e., "".
  • The TEXTSPLIT function splits the result at the pipe character (|), separating the numbers.

Extracting Specific Numbers from Text

You can use the formula above to extract all numbers from text in Excel. To extract specific numbers based on their position, wrap the formula with the CHOOSECOLS function.

The syntax is: =CHOOSECOLS(…, n)

For example, the following formula will return 1200 if the string in cell A1 is:

“The total cost of the project is $3500, with an initial deposit of $500 and additional expenses of $1200. The remaining balance is $1800.”

=CHOOSECOLS(VALUE(TEXTSPLIT(REGEXREPLACE(REGEXREPLACE(A1, "[^\d.]+", "|"), "^\||\|$", ""), "|")), 3)

The Role of VALUE in the Formulas

We used the VALUE function with all formulas to convert the text-formatted numbers returned by the formula into actual numbers so that we can use them in calculations.

It also plays an important role in formulas that use REGEXREPLACE. For example:

If a string ends with a number followed by a period, such as “Received $100.”, the formula will return “100.”. Using the VALUE function ensures that it returns just 100 without the period.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

How to Use OFFSET and XMATCH Functions Together in Excel

We often use the OFFSET and XMATCH functions together to match a value in...

How to Calculate Maximum Drawdown in Excel and Google Sheets

You can use the following dynamic array formula to calculate maximum drawdown (MDD) in...

Running Count with Structured References in Google Sheets

Running a count with structured references is achievable in Google Sheets tables using the...

More like this

How to Use OFFSET and XMATCH Functions Together in Excel

We often use the OFFSET and XMATCH functions together to match a value in...

Excel Tutorial: Calculate Nights Between Dates in Each Month

When working with date ranges in Excel, you might need to calculate how many...

How to Create a Case-Sensitive Pivot Table in Excel

As you may know, Pivot Tables in Excel are case-insensitive by default. To create...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.