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
These work with integers, decimals and negatives:
=REGEXEXTRACT(A2,"-*\d*.?\d+")
=VALUE(REGEXEXTRACT(A2,"-*\d*.?\d+"))
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+", ""))))
array formula variant:
=ARRAYFORMULA(IF(A1:A<>""; REGEXREPLACE(A1:A; "\D+"; )*1; ))
You could do a find and replace (Edit -> Find and Replace), searching for the regex [^\d]
(anything not a digit) and replacing with nothing.
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:
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+", ""))