Search code examples
regexlooker-studiore2

How can I extract only numbers from a string using Google Data Studio RegEx?


I am trying to extract only the numbers from a string; for example:

+36,145

Expected result:

36145

Solution

  • For Chart-level fields, adding the CAST function and setting the field AS NUMBER ensures that it can be aggregated as required, when set as a metric (REGEXP_REPLACE defaults to a TEXT field):

    CAST(REGEXP_REPLACE(X, "(\\+|,)", "") AS NUMBER )
    

    Google Data Studio Report and GIF to elaborate: