Home > Net >  Google Sheets Extracting Digits with Regex
Google Sheets Extracting Digits with Regex

Time:02-11

I've been stuck on this for a while now. I want to extract only digits (numeric values) from the cell, but for some reason it does not extract if it contains a single digit. See below for more details:

String End Result
$8 - added to cart 8

Formula used: =REGEXEXTRACT(J92,"\d .?\d ")

The formula would also grab digits that contain decimals. For some reason, this formula works if the string was "$81 - added to cart". Can someone help me fix this so it would work on strings with a single digit?

Thanks

CodePudding user response:

This should do what you're expecting:

=IFERROR(REGEXEXTRACT(J92,"[\d\.,] ")*1)

Note that I've included both decimal points and commas here and then multiplied by 1 to convert the extracted string to a real value. If the source string has no matches, IFERROR will return null.

However, I'll note that doing this cell by cell is inefficient when an array formula could likely process the entire range. If you're interested in such an approach, share a link to the spreadsheet (or to a copy of it) below.

CodePudding user response:

This regular expression looks for one or more digits, maybe more things, and finally one or more digits. There are several regex for what you are doing. I recommendend that you use the following: "\d (.\d )?"

  • Related