nelac123 nelac123 - 4 months ago 57
Java Question

Parsing a URL using a regular expression

I have been trying to parse an address from a string and have had partial success for select strings....

Currently I have

Pattern regex = Pattern.compile("[/].*[a-zA-Z](?=\/|:|)", Pattern.DOTALL)

On the input string
the current pattern gets me
(which is somewhat correct) however when I try the input string
it gives me

What I am trying to do is parse the address so that it ends before
, or whitespace

I did also come up with

Pattern regex = Pattern.compile("[.*/][^/][a-z].*[a-zA-Z](?=\/|:|)", Pattern.DOTALL)

and it works (partially) with
giving me

What am I doing wrong?


Try this regex ^.*?\/\/([^:\/\s]+), the part you're searching is stored in group 1.

Pattern pattern = Pattern.compile("^.*?\\/\\/([^:\\/\\s]+)");
Matcher matcher = pattern.matcher("your input url");
while (matcher.find()) {
    System.out.println("Domain: " +;

EDIT: Fixed the extra backslash issue by matching // before the expression.