Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match base url regex

Tags:

regex

Examples:

http://example.com ---> http://example.com
ftp://username:[email protected] ---> ftp://username:[email protected]
http://example.com/path/to/somehwere ---> http://example.com
http://example.co.uk ---> http://example.co.uk
example.co.uk ---> example.co.uk
example.co.uk?user=John&pass=1234 ---> example.com.uk

I tried this:

.*?(:\/{2})?.*?(?=\/|$|\?)

its working for the urls without protocols but cause of the (?=\/|$|\?) it stops at http:

Cany you help me fix my regex? or suggest better?

like image 703
biox Avatar asked Dec 04 '22 08:12

biox


1 Answers

I believe this lookahead based regex should for you:

^.+?[^\/:](?=[?\/]|$)
like image 183
anubhava Avatar answered Dec 21 '22 19:12

anubhava