If the string contains a valid URL then you could use a regex like (Perl quoting):
/^
(?:\w+:\/\/)?
[^:?#\/\s]*?
(
[^.\s]+
\.(?:[a-z]{2,}|co\.uk|org\.uk|ac\.uk|org\.au|com\.au|___etc___)
)
(?:[:?#\/]|$)
/xi;
Results:
url: https://foo.com/bar
matched: foo.com
url: http://www.foo.com#bar
matched: foo.com
url: http://bar.foo.com
matched: foo.com
url: ftp://foo.com
matched: foo.com
url: ftp://www.foo.co.uk?bar
matched: foo.co.uk
url: ftp://www.foo.co.uk:8080/bar
matched: foo.co.uk
For Java it would be quoted something like:
"^(?:\\w+://)?[^:?#/\\s]*?([^.\\s]+\\.(?:[a-z]{2,}|co\\.uk|org\\.uk|ac\\.uk|org\\.au|com\\.au|___etc___))(?:[:?#/]|$)"
Of course you'll need to replace the etc part.
Example Perl script:
use strict;
my @test = qw(
https://foo.com/bar
http://www.foo.com#bar
http://bar.foo.com
ftp://foo.com
ftp://www.foo.co.uk?bar
ftp://www.foo.co.uk:8080/bar
);
for(@test){
print "url: $_\n";
/^
(?:\w+:\/\/)?
[^:?#\/\s]*?
(
[^.\s]+
\.(?:[a-z]{2,}|co\.uk|org\.uk|ac\.uk|org\.au|com\.au|___etc___)
)
(?:[:?#\/]|$)
/xi;
print "matched: $1\n";
}