javaregexsplit

Pattern matching of a url path


I have the below urls which I need to parse:

a) https://url:port/abc
b) https://url:port/abc/{uid}
c) https://url:port/abc/{uid}/def

To figure out the type of the url (a, b or c), I am doing:

Pattern a = Pattern.compile(".*\\/abc$");
Pattern b = ??
Pattern c = Pattern.compile(".*\\/abc\\/(.*?)\\/def$");

Patterns a and c are working fine. Though I am not sure what pattern can I use so that the exact url of type b can be matched, without having to rely on order of the matches.


Solution

  • You can use this one:

     .*\\/abc\\/([^\\/]*)$
    

    Demo

    Nor the part after the slash: ([^\\/]*)$ it allows any number of characters that are not slashes, therefore it allows uid but not another part of the path.