Based on "I have multiple line string" I am assuming you have one string which contains data like:
String data = "A00233400193445 123394001\n" +
"A83485800193471 982001134";
In such case your pattern ^A[0-9]{6}(001)
is quite close to final solution, but you will need
- to use
MULTILINE
flag which lets ^
and $
to match not only start and end of entire text, but start and end of each line. That flag can be enabled by (?m)
syntax which starts section for regex where flag should be applied.
- to store somewhere match of
A[0-9]{6}
part, which can be done by capturing-group, like you did for (001)
part (probably unnecessarily here since you don't really want to reuse that match anywhere).
So your final solution can look like
String data =
"A00233400193445 123394001\n" +
"A83485800193471 982001134";
data = data.replaceAll("(?m)(A[0-9]{6})001","$1002"); //$1 represents match of group 1 (A[0-9]{6})
System.out.println(data);
Output:
A00233400293445 123394001
A83485800293471 982001134