magna_nz
magna_nz

Reputation: 1273

splitting URL regex after 4th slash

I'm trying to split a URL into chunks. What I want is everything up until the 5th / .

I've tried looking around but I'm new to regex and I'm getting overwhelmed a bit.

url example is:

http://daniel.mirimar.net.nz/Sites/reginald/DDD/CD

So what I'd like from here is: http://daniel.mirimar.net.nz/Sites/reginald/

How can I do this?

Upvotes: 0

Views: 1643

Answers (3)

Shar1er80
Shar1er80

Reputation: 9041

Sometimes regex can be a little overwhelming, especially if you're not familiar with it. It even can make code more difficult to read (Disadvantages of using Regular Expressions). Now, don't get me wrong, I like to use regex when the task is simple enough for it. IMO, you're better off solving this without regex. You can design a method to find the index location of the 5th "/" and then just return the substring.

Something like:

public static void main(String[] args) {
    String url = "http://daniel.mirimar.net.nz/Sites/reginald/DDD/CD";
    System.out.println(substringNthOccurrence(url, '/', 5));
}

public static String substringNthOccurrence(String string, char c, int n) {
    if (n <= 0) {
        return "";
    }

    int index = 0;
    while (n-- > 0 && index != -1) {
        index = string.indexOf(c, index + 1);   
    }
    return index > -1 ? string.substring(0, index + 1) : "";
}

Results:

http://daniel.mirimar.net.nz/Sites/reginald/

Upvotes: 0

gwillie
gwillie

Reputation: 1899

Short and concise is always nice

(?:.+?/){4}
  • (?: -- open non-capturing group
  • .+?/ -- lazily match anything till /
  • ) -- close non-capturing group
  • {4} -- repeat four times

Upvotes: 3

shA.t
shA.t

Reputation: 16968

Use a regex like this:

^.*?\/\/[^\/]*\/[^\/]*\/[^\/]*

or

^.*?\/(\/[^\/]*){3}

And for checking without CRLF and URL with fewer parts:

^.*?\/(\/[^\/\n\r]*){1,3}

You can be more specific by this:

^https?:\/(\/[^\/\n\r]*){1,3}

Upvotes: 0

Related Questions