0

I need to get a certain string value after a slash "/" but I am having trouble because the format value of the string is not always the same.

The value of string are the ff:

String raw = "Announcements/announcement_id)";

All I need here is the announcement_id which can be obtain with the use of lastIndexOf("/")+1 but this is not always the format of the string it could be also like this.

String raw = "Announcements/announcement_id/Comments/comment_id)";

Here all I need are both announcement_id and comment_id.

For the last part string value could be also like this.

String raw = "Announcements/announcement_id/Comments/comment_id/Replies/reply_id)";

Here all I need are both comment_id and reply_id.

How do I get this right like using if else statement.

if(has only one slash)
   use lastIndexOf("/")+1 to get the id

else if (has three or more slashes)
   get the value after second to the last forward slash and last forward slash
4
  • 1
    use String.split("/") and check the length of the returned array
    – Stultuske
    Commented Feb 25, 2019 at 11:30
  • 1
    Why no announcement_id in last example? I think a regex would work then Commented Feb 25, 2019 at 11:30
  • @XtremeBaumer I do not need announcement_id as of now Commented Feb 25, 2019 at 11:32
  • 1
    In that case, Stultuske's idea is what you need Commented Feb 25, 2019 at 11:32

4 Answers 4

4

You can use this regex, which allows optional parts in string and appropriately captures announcement_id, comment_id and reply_id

(?:Announcements/([^/]*))(?:/Comments/([^/]*))?(?:/Replies/([^/]*))?\)

Each of which you can access by group1, group2 or group3 respectively.

And as each part is made optional, hence it allows you to optionally have reply_id without having commend_id and as per your samples, I have made announcement_id as mandatory even which you can make it optional.

Demo

Check out this sample Java code,

List<String> list = Arrays.asList("Announcements/announcement_id)",
        "Announcements/announcement_id/Comments/commend_id)",
        "Announcements/announcement_id/Comments/comment_id/Replies/reply_id)");

Pattern p = Pattern.compile("(?:Announcements/([^/]*))(?:/Comments/([^/]*))?(?:/Replies/([^/]*))?\\)");

list.forEach(x -> {
    Matcher m = p.matcher(x);
    if (m.matches()) {
        System.out.println("For string '" + x + "' --> announcement_id: " + m.group(1) + ", commend_id: "
                + m.group(2) + ", reply_id: " + m.group(3));
    }
});

Which prints the data appropriately as found,

For string 'Announcements/announcement_id)' --> announcement_id: announcement_id, commend_id: null, reply_id: null
For string 'Announcements/announcement_id/Comments/commend_id)' --> announcement_id: announcement_id, commend_id: commend_id, reply_id: null
For string 'Announcements/announcement_id/Comments/comment_id/Replies/reply_id)' --> announcement_id: announcement_id, commend_id: comment_id, reply_id: reply_id
2
String s = "Announcements/announcement_id/Comments/comment_id/Replies/reply_id";
String[] h = s.split("\\/");
switch (h.length) {
case 2:
    System.out.println(h[1]);
    break;
case 4:
    System.out.println(h[1] + " " + h[3]);
    break;
case 6:
    System.out.println(h[3] + " " + h[5]);
    break;
default:
    break;

}

This code also provides a way on how to continue after the split, as well as access the elements you need

11
  • 1
    Wow! Thanks you make it more simple. I just want to ask why \\/ and not just / ? Commented Feb 25, 2019 at 11:38
  • 1
    String#split() works with a regular expression. \` the double backslash escapes the /` forward slash and makes sure that you don't get some random results Commented Feb 25, 2019 at 11:40
  • 1
    In Java regex, you don't need to escape / (unlike in most other languages/tools where you need to do as default regex delimiter is / which you can even change to avoid escaping / there as well) and can simply split it like this s.split("/"); Commented Feb 25, 2019 at 11:58
  • 1
    Okay I check it now and the IDE says that regex cause a redundancy which means split("\\/") and split("/") is always the same. Commented Feb 25, 2019 at 12:22
  • 1
    @PushpeshKumarRajwanshi that might be true for Java, but PHP and JavaScript, can't parse an unescaped forward slash Commented Feb 25, 2019 at 13:47
1

Your order of pathParams are predifined, so use split and parse the array.

 String s = "Announcements/announcement_id/Comments/comment_id/Replies/reply_id)";
 String [] pathParams = s.split("/");
1
  • Thanks this is what exactly I need. Commented Feb 25, 2019 at 11:36
0

The following method is for more paths

String s = "Announcements/announcement_id/Comments/comment_id/Replies/reply_id)";
String[] paths = s.split("/");

if (paths.length % 2 == 0) {
  if (paths.length == 2) {
    System.out.println(paths[1]);
  } else {
    int lastPos = paths.length - 1;
    int beforeLastPos = lastPos - 2;
    System.out.println(paths[beforeLastPos] + " " + paths[lastPos]);
  }
}

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.