-1

I want to replace only the last occurrence of the pattern {id} or {objId} or {anything else} that appears after the last occurrence of / with xyz in Java.

Input:

/test/data/{id}/moreData/{id}
/test/data/{id}/moreData/{objId}
/test/data/{id}/moreData/{anything else}

Expected output:

/test/data/{id}/moreData/xyz
/test/data/{id}/moreData/xyz
/test/data/{id}/moreData/xyz

Please suggest a regex for this.

Pushpesh Kumar Rajwanshi
  • 17,308
  • 2
  • 17
  • 34
Mohan S
  • 47
  • 8
  • 2
    If it's `java` why are you using the `javascript` tag? And what have you tried so far, is there some code? – cнŝdk Mar 22 '19 at 07:41
  • 1
    Possible duplicate of [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – Biffen Mar 22 '19 at 08:22

1 Answers1

3

As clarified in comments, you seem to be looking to replace the last placeholder that appears after a slash /. For this you can use following regex,

[^/]+$

Here, [^/] is a negated character class which captures any character except a slash / and + means one or more characters and $ means end of string which will ensure this will match any text that appears in end of string not containing /. So just match using it and replace with xyz or any string of your choice.

Demo

Java code,

List<String> list = Arrays.asList("/test/data/{id}/moreData/{id}","/test/data/{id}/moreData/{objId}","/test/data/{id}/moreData/{anything else}");
list.forEach(x -> System.out.println(x + " --> " + x.replaceAll("[^/]+$", "xyz")));

Prints,

/test/data/{id}/moreData/{id} --> /test/data/{id}/moreData/xyz
/test/data/{id}/moreData/{objId} --> /test/data/{id}/moreData/xyz
/test/data/{id}/moreData/{anything else} --> /test/data/{id}/moreData/xyz
Pushpesh Kumar Rajwanshi
  • 17,308
  • 2
  • 17
  • 34
  • Thanks Pushpesh for your inputs. I will try this. A small change in my question, that it's not only ID, it can be any identifier within the curly braces. Only the last pattern should be replaced. Input : /data/{objId}/moreData/{ID}/subData/{subId} Output : /data/{objId}/moreData/{ID}/subData/xyz – Mohan S Mar 22 '19 at 08:49
  • @Pushpesh - My point is, i won't be knowing the URI pattern, it's depends on some external implementation. Since it's a resourceURI, the assumption is the target resourceID will always be on the end of the string. So, this can be either {id}, {objId} or {something_else} . I need to replace this. Also, will it help if we use $ to try specify that i need to replace only the {*} at the end of string – Mohan S Mar 22 '19 at 08:56
  • @MohanS: Ok, I think now your question has become precise and if I am correct, you want to match the last string after `/` and replace that with some string. In this case I'll suggest another regex `[^\/]+$` which will be any text other than `/` just before end of string. [Check this demo](https://regex101.com/r/F4Av5Y/4) and let me know if this works better. – Pushpesh Kumar Rajwanshi Mar 22 '19 at 09:03
  • This helps, Thanks a lot. You used the character class to match for / and text following that. And $ you are considering only the last pattern. This is what i was looking for exactly. Thank you. I will also go through the earlier regex provided and understand how it works. – Mohan S Mar 22 '19 at 09:15
  • @MohanS: Yes, you are partially correct. Let me update my answer and add the explanation there itself. – Pushpesh Kumar Rajwanshi Mar 22 '19 at 09:17
  • Thankyou. One more question. Can we update this pattern to replace the last section following / only if it has content wrapped within { } . Not sure if this can be done. The below inputs should not be replaced. Input 1 : /resource/{rID}/text/sometext Input 2 : /resoruce/{objID}/text/[someId] – Mohan S Mar 22 '19 at 09:24
  • @MohanS: Updated my answer. Let me know for any queries further. – Pushpesh Kumar Rajwanshi Mar 22 '19 at 09:28