1

I have two string in which I have to sorten urls. I want a regex pattern to extract them

https://l.facebook.com/l.php?u=http%3A%2F%2Febay.to%2F2EyH7Nq&h=ATNHM5kACc4rh_z68Ytw__cNCzJ63_iPezd_whc0PjcsN4qj1PfdJgFXyrOKM3-biqPm7eAXTOc5LD6r-7JXhRsqsqEHUs0jaJkjvm_QWf6sqbHQmS63q6P0_NcQoUa86Az5EttNT9xJb_evKBaiFxW7e7v2afJQn2zNxz5lQ8xgxhMcEFuJ3hUiSYUMEemKFB2LSIgAZFibRv4GeRrTk8hxFaArkBuAhQaXQFd4jX-aQuUYhjD0ErV5FY-D4gFMpb0lFCU7SyBlRpkUuOcHVjwjxN-_g6reMYwo8loAJnJD
/redirect?q=http%3A%2F%2Fgoo.gl%2FIW7ct&redir_token=PV5sR8F7GuXT9PgPO_nkBFLABQx8MTUxNjA3OTY5MEAxNTE1OTkzMjkw&v=7wmIyD1fM4M&event=video_description

Output will be from 1st and 2nd link:-

http%3A%2F%2Febay.to%2F2EyH7Nq
http%3A%2F%2Fgoo.gl%2FIW7ct

Please help me out. I have already used:- (http|https).*?&

but its not working on first url.

Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
anshul
  • 31
  • 5
  • possible dupe of https://stackoverflow.com/questions/33211233/how-to-detect-and-get-url-on-string-javascript – Anand G Jan 15 '18 at 06:14

3 Answers3

2

You can try this:

=(https?[^&]*)

Demo

If lookbehind is possible in your flavour of regex then you may try this as well which will ensure to not capture the equal sign:

(?<=)(https?[^&]*)

Demo 2

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
  • Might be worth adding a capture group to make it easy to ditch the `=`... Or look-behind ;) – MatBailie Jan 15 '18 at 06:16
  • You are right MatBailie, in the mean time I was trying to just match the desired thing thus come the lookbehind regex. Btw added capture group to the first solution. – Mustofa Rizwan Jan 15 '18 at 06:18
1

Try this regex !

I am also attach the output of the regex through regex101.

http%3A%2F%2F(.*)%2F(.*[^&])(?=&)

output

Usman
  • 1,983
  • 15
  • 28
0

You can use this pattern to only capture goo.gl and ebay.to links:

(http%3A%2F%2F(ebay\.to|goo\.gl)%2F[^&]*)&

Demo

user4020527
  • 1
  • 8
  • 20