Extract digits from string - Google spreadsheet

前端 未结 6 1001
情深已故
情深已故 2021-02-01 17:19

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

相关标签:
6条回答
  • 2021-02-01 17:58

    These work with integers, decimals and negatives:

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

    0 讨论(0)
  • 2021-02-01 18:04

    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+", ""))))
    

    0 讨论(0)
  • 2021-02-01 18:07

    array formula variant:

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

    0 讨论(0)
  • 2021-02-01 18:12

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

    0 讨论(0)
  • 2021-02-01 18:15

    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:

    0 讨论(0)
  • 2021-02-01 18:16

    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+", ""))
    

    0 讨论(0)
提交回复
热议问题