6

I am trying to extract the first part of the path of a URL. For example: from http://foo.com/bar/1/2/3 I want to extract bar. Here is the code I am using:

private static String getFirstPartOfPath(String url)
{
    Pattern pattern = Pattern.compile("^https?://[^/]+/([^/]+)/*$");
    Matcher matcher = pattern.matcher(url);
    if(matcher.find())
    {
        System.out.println(matcher.group(1));
    }
    return null;
}

However, this does not match the most trivial url listed above, as in

public static void main(String[] args)
{
    getFirstPartOfPath("http://foo.com/bar/1/2/3");
}

prints nothing. At first glance the pattern string seems clear and like it should obviously work. What is going wrong?

1 Answer 1

8

Not matching because your regex is not correct. You have /* in the end that is not same as /.*.

Use this regex:

Pattern pattern = Pattern.compile("^https?://[^/]+/([^/]+)/.*$");

Or remove anchor $:

Pattern pattern = Pattern.compile("^https?://[^/]+/([^/]+)/");
Sign up to request clarification or add additional context in comments.

1 Comment

Got it, thanks. I will accept when SO lets me in 9 min.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.