Reputation: 20617
I want to split a url into two from last /
, for example:
http://github.com/members
into http://github.com
and members
So far I have tried https://regex101.com/r/SiVvRA/1, which just gives me the second part. Any pointers?
Upvotes: 3
Views: 3054
Reputation: 425033
To split on the last slash, split on slash not followed by another slash, using this regex:
/(?!.*/)
See live demo of this regex matching the last slash
See live demo of this regex being used in this Java to split:
String url = "http://github.com/members";
String[] parts = url.split("/(?!.*/)");
Arrays.stream(parts).forEach(System.out::println);
Output:
http://github.com
members
Upvotes: 3
Reputation: 214957
Depending what language you are using, you may simply use a built in split
method; But for a regex solution, you can match the string into two groups and extract them accordingly:
^(.*)/([^/]*)$
https://regex101.com/r/SiVvRA/2
Upvotes: 1