Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract digits from string - Google spreadsheet

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 
like image 375
thanos.a Avatar asked Jan 19 '17 21:01

thanos.a


People also ask

How do I extract a number from a text string in Google Sheets?

We use the '' symbol so that Google Sheets understands that we mean the meta-character 'd' and not the letter 'd'. So if you want to extract a single numeric digit from a string, you use the expression 'd'. But if you want to extract more than one digits, you can use the expression 'd+'.

How do I extract a number from a string in Excel?

Extract Numbers from String in Excel (using VBA) Since we have done all the heavy lifting in the code itself, all you need to do is use the formula =GetNumeric(A2). This will instantly give you only the numeric part of the string.


2 Answers

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

like image 137
Wiktor Stribiżew Avatar answered Oct 07 '22 11:10

Wiktor Stribiżew


These work with integers, decimals and negatives:

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

regex negative decimals - google sheets function

like image 23
ZygD Avatar answered Oct 07 '22 10:10

ZygD