Abhay
Abhay

Reputation: 845

Need to use regex to extract a part of a string

I'm a regex noob that's trying to use the regexp_extract() function in data studio to extract part of a string. Could you help me out?

I need to extract the part of the string that comes after 'May'. Everything before 'May' is exactly the same across all campaigns.

I've tried googling the solution and killed a lot of time on regexer.com but i can't figure it out

Current Campaign Name:                       
Xxxxx_xxxxx_PKN_Trueview_24th MayComedy Movie Fans18-24 
Xxxxx_xxxxx_PKN_Trueview_24th MaySouth Asian Film Fans18-24 
Xxxxx_xxxxx_PKN_Trueview_24th MayCricket Enthusiasts18-24   
Xxxxx_xxxxx_PKN_Trueview_24th MayMotorcycle Enthusiasts18-24    

Expected Campaign Names:
Comedy Movie Fans18-24
South Asian Film Fans18-24
Cricket Enthusiasts18-24
Motorcycle Enthusiasts18-24

EDIT: I'm trying to use this in data studio in the REGEXP_EXTRACT(Campaign,"regex_code_here") function. I think the acceptable syntax is re2.

Upvotes: 2

Views: 651

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You may actually use REGEXP_REPLACE here to remove all before and including May:

REGEXP_REPLACE(Campaign, '.*May', '')

See the regex demo:

enter image description here

Upvotes: 1

Code Maniac
Code Maniac

Reputation: 37775

You can use replace

  • ^.*?May - Match everything up-to first occurrence of May

  • "$`" - replace with portion that follows substring Ref

    let arr = ["Xxxxx_xxxxx_PKN_Trueview_24th MayComedy Movie Fans18-24","Xxxxx_xxxxx_PKN_Trueview_24th MaySouth Asian Film Fans18-24","Xxxxx_xxxxx_PKN_Trueview_24th MayCricket Enthusiasts18-24","Xxxxx_xxxxx_PKN_Trueview_24th MayMotorcycle Enthusiasts18-24"]
    
    let op = arr.map(str=> str.replace(/^.*?May/g, "$`"))
    
    console.log(op)

Upvotes: 0

virolino
virolino

Reputation: 2227

The regex you need is this:

(?<=May).*$

Test it here.

Upvotes: 0

Related Questions