38

In Google spreadsheets, I need a formula to extract all digits (0 to 9) contained into an arbitrary string, that might contain any possible character and put them into a single cell.

Examples (Input -> Output)

d32Ελληνικάfe9j.r/3-fF66 -> 329366
h01j2j3jFxF$$4j5j6j7j8j9 -> 0123456789
player0
  • 124,011
  • 12
  • 67
  • 124
thanos.a
  • 2,246
  • 3
  • 33
  • 29

6 Answers6

74

You may replace all non-digit characters using the \D+ regex and an empty string replacement with

=REGEXREPLACE(A11,"\D+", "")

or with casting it to a number:

=VALUE(REGEXREPLACE(A11,"\D+", ""))

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
28

These work with integers, decimals and negatives:

=REGEXEXTRACT(A2,"-*\d*\.?\d+")
=VALUE(REGEXEXTRACT(A2,"-*\d*\.?\d+"))

regex negative decimals - google sheets function

ZygD
  • 22,092
  • 39
  • 79
  • 102
  • This is a usefull answer and thansk for posting. However the question is not about extracting numbers but just the digits from 0 to 9. – thanos.a Aug 01 '19 at 07:47
  • 3
    I found this question as the top result from my search for how to parse numbers in a Google Sheet. I know that it's not technically the correct answer to the question but it's the answer that I needed. – magikid Dec 16 '19 at 14:33
5

If some of source cells may contain only numbers it is safer to convert the value to text first, then regexreplace. Otherwise it produces error.

Result as text

=REGEXREPLACE(TO_TEXT(C1),"\D+", "")

Result as number

=VALUE(REGEXREPLACE(TO_TEXT(C1),"\D+", ""))

The same for whole column

=ARRAYFORMULA(IF(LEN(C1:C), VALUE(REGEXREPLACE(TO_TEXT(C1:C),"\D+", ""))))

enter image description here

Kristjan Adojaan
  • 527
  • 7
  • 10
2

If you wanted to extract with decimal points, you could use regexextract:

=VALUE(REGEXEXTRACT(B4,"[0-9]*\.[0-9]+[0-9]+"))

Example to extract digits, a decimal and 2 significant digits:

=VALUE(REGEXEXTRACT(A1,"[0-9]*\.[0-9]+[0-9]+"))

Output:

Extract just digits, decimal, and 2 significant digits

nk abram
  • 1,531
  • 2
  • 11
  • 19
  • This code works only for special cases like this and it will not for my examples. – thanos.a Jan 06 '19 at 23:15
  • Thought others might benefit from additional use cases and regextract can be more useful than the above example. – nk abram Jan 07 '19 at 08:31
  • Might be but solves a different problem such as: "Retrive value from values with unit of measure. I would let admins decide. – thanos.a Jan 08 '19 at 09:32
  • 2
    I needed to extract with decimals too, this works in random sentences: =REGEXEXTRACT(H13, "\d+.\d+") – syonip Jan 27 '19 at 17:19
1

array formula variant:

=ARRAYFORMULA(IF(A1:A<>""; REGEXREPLACE(A1:A; "\D+"; )*1; ))

0

player0
  • 124,011
  • 12
  • 67
  • 124
0

You could do a find and replace (Edit -> Find and Replace), searching for the regex [^\d] (anything not a digit) and replacing with nothing.

enter image description here

Alex
  • 12,078
  • 6
  • 64
  • 74