static String AdrPattern="http://www.([^&]+)\\.com\\.*";
static Pattern WebUrlPattern = Pattern.compile (AdrPattern);
static Matcher WebUrlMatcher;
WebUrlMatcher = WebUrlPattern.matcher ("keyword");
if(WebUrlMatcher.matches())
String extractedPath = WebUrlMatcher.group (1);
Considering above codes, My aim is to extract the domain name from the URL and dismiss the rest. But the trouble is that, first of all, if the URL has deeper path, it will not ignore it and second, it does not work for all URL with .com
extension.
For example, if the URL is http://www.lego.com/en-us/technic/?domainredir=technic.lego
, the result will not be lego
but lego.com/en-us/technic/?domainredir=technic.lego
.
Use
You escaped the final dot, and it was treated as a literal, and
matches
could not match the entire string. Also, the first dot must be escaped.Also, to make the regex a bit more strict, you can replace the
[^&]+
with[^/&]
.UPDATE:
Or, with
\G
: