It looks like you want something like this:
public static String replaceWith(String input, String repl) {
return Regex.Replace(
input,
@"(?<=~)[A-Z$%]+",
repl
);
}
The (?<=…)
is what is called a lookbehind. It's used to assert that to the left there's a tilde, but that tilde is not part of the match.
Now we can test it as follows (as seen on ideone.com):
Console.WriteLine(replaceWith(
"X(P)~AK,X(MV)~AK", "AP"
));
// X(P)~AP,X(MV)~AP
Console.WriteLine(replaceWith(
"X(P)~$B,X(MV)~$B", "C$"
));
// X(P)~C$,X(MV)~C$
Console.WriteLine(replaceWith(
"X(P)~THIS,X(MV)~THAT", "$$$$"
));
// X(P)~$$,X(MV)~$$
Note the last example: $
is a special symbol in substitutions and can have special meanings. $$
actually gets you one dollar sign.
Related questions