Graham
Graham

Reputation: 1960

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]*')

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

Upvotes: 1

Views: 4706

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

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]).

Upvotes: 6

Related Questions