$re = '~:// # end of prefix
.*? # anything or nothing, but not greedy
( # only capture group you want
[a-z0-9\-]+ # left side of TLD, only allowed chars
\. # dot between left and right
[a-z0-9\-]+ # right side of TLD
)
(?:$|/) # bounded by EOL or / (non-capturing group)~mx';
$str = 'http://www.localpetcare.com
https://app.clickfunnels.com/users/sign_in
https://www.timetopet.com/login#
https://www.bankatfirst.com/content/first-financial-bank/home/
https://app.truecoach.co/login
https://shield.mycoseva.com/qcoseva/ordernow.dhtml
https://d.comenity.net/as/authorization.oauth2?client_id=ngac
http://www.example.com/test/if/domains/shows/up/later/www.google.com
http://two.levels.domain.com/do/you/still/want/just/domain.com?';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php