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/12345
becomes/users/XXXXX
/users/234567/summary
becomes/users/XXXXXX/summary
/api/v1/summary/5678
becomes/api/v1/summary/XXXX
http://example.com/api/v1/summary/5678/single
becomeshttp://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?