问题
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
回答1:
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+", ""))
回答2:
These work with integers, decimals and negatives:
=REGEXEXTRACT(A2,"-*\d*.?\d+")
=VALUE(REGEXEXTRACT(A2,"-*\d*.?\d+"))
回答3:
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+", ""))))
回答4:
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:
回答5:
array formula variant:
=ARRAYFORMULA(IF(A1:A<>""; REGEXREPLACE(A1:A; "\D+"; )*1; ))
回答6:
You could do a find and replace (Edit -> Find and Replace), searching for the regex [^\d]
(anything not a digit) and replacing with nothing.
来源:https://stackoverflow.com/questions/41752141/extract-digits-from-string-google-spreadsheet