I want to replace only numeric section of a string. Most of the cases it's either full URL or part of URL, but it can be just a normal string as well.
/users/12345becomes/users/XXXXX/users/234567/summarybecomes/users/XXXXXX/summary/api/v1/summary/5678becomes/api/v1/summary/XXXXhttp://example.com/api/v1/summary/5678/singlebecomeshttp://example.com/api/v1/summary/XXXX/single
Notice that I am not replacing 1 from /api/v1
So far, I have only following which seem to work in most of the cases:
input.replaceAll("/[\\d]+$", "/XXXXX").replaceAll("/[\\d]+/", "/XXXXX/");
But this has 2 problems:
- The replacement size doesn't match with the original string length.
- The replacement character is hardcoded.
Is there a better way to do this?