HansOlo
HansOlo

Reputation: 3

Extract first two characters after trailing slash using regex

I can extract all characters after the trailing slash of an url using this regex:

([^/]*)$

How can I extract only the first two characters after the trailing slash? I want to use it within Google Data Studio.

Upvotes: 0

Views: 85

Answers (1)

JvdV
JvdV

Reputation: 75840

Maybe some simple lookahead and lookbehind.

(?<=\/)..(?!.*\/)

See the demo

Or:

\/\K..(?!.*\/)

See the demo


If you want to be more verbose/explicit, maybe change .. into [^\/]{2}.


Update: You now mentioned you are using Google Data Studio. Lookarounds won't work in that case. Have a try with:

([^\/]{2})[^\/]*$

And grab the 1st capture group. See the demo

Upvotes: 1

Related Questions