To replace a string that occurs between two other strings, a common approach is to capture the two bounding strings and then the replacement expression puts back the two captured strings with the new wanted text in the middle.
Using the RegEx (K11\|).*(\|P) captures the K11| and the |P in groups 1 and 2. The text between them is matched by the .* but it is not captured.
The question is not clear on what the replacement should be, so lets assume that it is NewText.
The replacement expression should then be \1NewText\2 or $1NewText$2 depending on the exact RegEx version being used.
C# code to perform the change could be as follows. Note that the backslash characters in the strings need to be doubled when putting them the C# strings.
string source = "MSH|^~\\&|dgdgd|MSH6TOMSH4|Instrument|MSH4toMSH6|20230921104820+01:00||RSP^K11^RSP_K11|QPC0amoCwk+2uSHidYKB+Q|P|2.5.1||||||UNICODE UTF-8|||LAB-27R^";
string regex = "(K11\\|).*(\\|P)";
string replace = "$1NewText$2";
string output = Regex.Replace(source, regex, replace);
Console.WriteLine($"Was: '{source}'");
Console.WriteLine($"Now: '{output}'");
The output from this code is:
Was: 'MSH|^~\&|dgdgd|MSH6TOMSH4|Instrument|MSH4toMSH6|20230921104820+01:00||RSP^K11^RSP_K11|QPC0amoCwk+2uSHidYKB+Q|P|2.5.1||||||UNICODE UTF-8|||LAB-27R^'
Now: 'MSH|^~\&|dgdgd|MSH6TOMSH4|Instrument|MSH4toMSH6|20230921104820+01:00||RSP^K11^RSP_K11|NewText|P|2.5.1||||||UNICODE UTF-8|||LAB-27R^'
A comment on the question states that
K11\|(.*)\|P still returns QPC0amoCHidY
Where the text QPC0amoCHidYis part of the string between K11| and |P. In this ReGex the text that is captured is the text the should be replaced, the original K11| and |P are thus lost. I do not know why the rest of the text between the two strings (i.e. the +2uSHidYKB+Q) does not appear, but I suspect that something extra is being done in the code.
K11|and|Pbut not just extracting the nth column of that CSV string ?/K11\|(.*?)\|P/g) works on all flavors listed on regex101, including ".NET (C#)". Maybe try without the?:K11\|(.*)\|P?Regex.Replace? See ideone.com/9UwCu0