Match base url regex

regex

Solution

This lookahead based regex should for you:

^.+?[^\/:](?=[?\/]|$)

RegEx Demo

Lookahead assertion `(?=[?\/]|$)` makes sure to stop matching when next character is `/` or `?` or there is line end.

Problem

Examples: ``` http://example.com ---> http://example.com ftp://username:password@example.com ---> ftp://username:password@example.com 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?

Original source