I have below code
String str = "/admin/3138/misc"; boolean isNumbers = str.chars().anyMatch(Character::isDigit); System.out.println(isNumbers); if (isNumbers) { str = str.replaceAll("[0-9]", "%d"); System.out.println("->" + str); }
Which gives output:
admin/%d%d%d%d/misc
I need:
admin/%d/misc
So from string if numbers found just replace with %d
one only, not every digit.
Advertisement
Answer
You need to change your regex to "[0-9]+"
if (isNumbers) { str = str.replaceAll("[0-9]+", "%d"); System.out.println("->" + str); }
Oracle’s doc: Quantifiers