user3554045 user3554045 - 1 year ago 107
Java Question

Java - Regex to replace dot between numbers Only

Regular expression to replace dot between two numbers Only.

for example replace 3.4 to 3xx4 ,,,,, 12.45 to 12xx45

replace all occurrences to above pattern from string

Dots may appear after sentence end, or at any other position; but i want to replace dot that appears between numbers only.

Answer Source

using parenthetical capture groups:

 input = input.replaceAll("(\\d+)\\.(\\d+)", "$1xx$2");

or, using lookarounds:

 input = input.replaceAll("(?<=\\d)\\.(?=\\d)", "xx");