I am trying to match a regex pattern to replace a particular string.
Sample Text : ABC/1111111031111111/0318*12345678
\/(\d{12,19})\/(0[1-9]|1[0-2])(\d{2})\*
I want to replace the 03 and 18 in /0318 with with "/1222" i.e (Dec-2022). I tried the string replaceAll method but that replaces all the matched characters in the provided sample string. Something like below;
Sample Code i tried:
sampleText.replace(matcher.group(2), "12");
sampleText.replace(matcher.group(3), "22")
How can i do this ?
| SAMPLE I/P | EXPECTED O/P |
|---|---|
| ABC/1111111031111111/0318*12345678 | ABC/1111111031111111/1222*12345678 |
| ABC/1852111031156311/1120*12345678 | ABC/1852111031156311/1222*12345678 |
.replaceAll("(/\\d{12,19}/)(?:0[1-9]|1[0-2])\\d{2}(\\*)", "$11222$2")works.’ABC’or that it contain exactly two forward slashes followed by at least so-many digits? I have these questions because you cast your question in terms of examples. Examples are fine for illustration but they are not a substitute for a precise statement of the question in words.