I have a message String that separated by different special characters. For example,
MSG|027052^CMXV~MB^M^1^2^MD~GUY^M^1^2^MD|O|^1^2^STD||||027052^SCHROPP~GUY^M^^^MD
Now according to user inputs like,
-> 1(1).1 - Means in this string first occurrence of '|'(Occurrence of '~').Occurence of '^' -> O/P: M
-> 1(1) - Means in this string first occurrence of '|'(Occurrence of '~') -> O/P: GUY^M^1^2^MD
-> 1 - Means in this string first occurrence of '|' -> O/P: 027052^CMXV~MB^M^1^2^MD~GUY^M^1^2^MD
Now inputs can be one of three. Now I have to replace found piece of string (ex. {I: 1(1)} O: GUY^M^1^2^MD) with other string.
Following is piece of code that give only pipe separated regex.
String originalMsg = "MSG|027052^CMXV~MB^M^1^2^MD~GUY^M^1^2^MD|O|^1^2^STD||||027052^SCHROPP~GUY^M^^^MD";
String msg = originalMsg.replaceAll("^((?:[^|]*\\|){1})[^|]*", "$1ABC" );
O/P: MSG|ABC|O|^1^2^STD||||027052^SCHROPP~GUY^M^^^MD
EDIT:
String msg = originalMsg.replaceAll("^((?:[^|]*\\|){1}([^~]+.){2}))[^|]*", "$1ABC" );
O/P: MSG|027052^CMXVABC|O|^1^2^STD||||027052^SCHROPP~GUY^M^^^MD
It will only replace '|' separated string. I want one shot strategy that will replace according to user inputs.