user10463769
user10463769

Reputation: 29

Using RegEx to extract a string in a URL

I've tried to search for this and I'm sure versions of this question have been asked, but I haven't been able to apply other answers to my case.

I need to use RegEx to extract a random string of characters and symbols that appears in the URL when an advertiser sends traffic to me.

The referring URL looks something like this, with the part I want to extract in bold:

https://adclick.g.doubleclick.net/pcs/click%**long-string-of-characters-and-symbols**https://www.mywebsite.com

That long string of characters and symbols (the hash) contains multiple % signs so I need the entire string after the first % sign, but before my website's URL.

I've been pulling my hair out on this and any help would be appreciated!

Upvotes: 0

Views: 196

Answers (1)

Neb
Neb

Reputation: 2280

You can use:

(?<=%).*(?=https)

How it works:

  • (?<=%) Positive lookbehind: search for a character preceeded by %
  • .* matches everything until...
  • (?=https): the first https occurs (Positive lookhead)

Upvotes: 2

Related Questions