Search code examples
regexextractlooker-studiore2

regexp extract in google data studio


I want to create a calculated field in google data studio which only returns the numbers at the beginning of a string which could look like this:

123-something-something1
or
32-something-something2
or
1234-somthing-something3

I thought that I had the right regex but it's only returning null which means the regex is wrong.

The regex's i've tried:

REGEXP_EXTRACT(Campagne, '.*')
REGEXP_EXTRACT(Campagne, '^[0-9].[0-9]*')
REGEXP_EXTRACT(Campagne, '[0-9].[0-9]*')
REGEXP_EXTRACT(Campagne, '^[0-9]*')
REGEXP_EXTRACT(Campagne, '[0-9]*')
REGEXP_EXTRACT(Campagne, '^[0-9].[0-9]*')
REGEXP_EXTRACT(Campagne, '[0-9].[0-9]*')
  • ^ this should stand for the beginning of the value
  • [0-9] should count any number
  • . should match any character
  • * counts as an multiplier for the previous character

Really hope someone can help me out since I can't get it to work


Solution

  • The point is to match and capture the digits in the beginning of the string. There must be 1) at least 1 digit and 2) the capturing group.

    You may extract the leading digits using

    REGEXP_EXTRACT(Campagne, '^([0-9]+)')
    

    The pattern matches:

    • ^ - start of a string
    • ([0-9]+) - Capturing group 1 (what you extract): one or more (+) digits ([0-9]).