Search code examples
regexgoogle-analyticslooker-studioregexp-replacere2

Replace / Delete everything after first + character in datastudio


I have a string looking like this (stored as an Event Action value from Google Analytics)

0+171235652++zu

or

122+115166747++en

I would like (with the use of calculate fields) create a new field that will show only the number before the 1st '+' character. So in those examples above

0 or 122

What I tried was (below), but it did not help, Any ideas?

REGEXP_REPLACE(Event Action, '(^\\+).*', '')

Solution

  • You may use

    REGEXP_EXTRACT(Event Action, '^([^+]+)')
    

    See the regex in action. The regex matches:

    • ^ - start of string
    • ([^+]+) - Capturing group 1: any one or more chars other than a + (you may use ([^+]*) if you want to also get empty match when a + is the first char).

    If you want a replacement function, you may use

    REGEXP_REPLACE(Event Action,"[+].*","")