C#中仅替换ReserveChar段的\"为",保留Alphabet段换行符
Solution 1: Regex with Match Evaluator (Robust for Variable Formatting)
This approach targets only the ReserveChar segment using regex, then unescapes the quotes within that segment without touching the Alphabet section's \n sequences.
string originalString = "\"alphabet\": \"dtSearch Alphabet File\\n\\n[Letters] // Original letter, lower case, upper case, unaccented\\n\",\"ReserveChar\": \"{\\\"Datasource\\\":[{\\\"text\\\":\\\"!\\\",\\\"value\\\":\\\"!\\\"},{\\\"text\\\":\\\"^\\\",\\\"value\\\":\\\"^\\\"},{\\\"text\\\":\\\"|\\\",\\\"value\\\":\\\"|\\\"},{\\\"text\\\":\\\"$\\\",\\\"value\\\":\\\"$\\\"}],\\\"macro\\\":\\\"@\\\",\\\"fuzzychar\\\":\\\"%\\\",\\\"stemming\\\":\\\"~\\\",\\\"phonic\\\":\\\"#\\\",\\\"shynonyms\\\":\\\"&\\\",\\\"wightchar\\\":\\\":\\\",\\\"matchdigitchar\\\":\\\"=\\\"}\""; // Regex pattern to capture the ReserveChar value (inside quotes) string regexPattern = @"""ReserveChar"":\s*""([^""]*(?:\\.[^""]*)*)"""; string result = Regex.Replace(originalString, regexPattern, match => { // Extract the content inside ReserveChar's quotes string reserveContent = match.Groups[1].Value; // Replace all escaped quotes (\") with regular quotes (") reserveContent = reserveContent.Replace(@"\""", @""""); // Reconstruct the ReserveChar segment with unescaped quotes return $"\"ReserveChar\": \"{reserveContent}\""; }); Console.WriteLine(result);
How it works:
- The regex targets the exact
ReserveCharkey and captures its entire escaped value. - The match evaluator processes only the captured value, replacing
\"with". - The
Alphabetsection remains completely unchanged, preserving critical\nsequences.
Solution 2: Non-Regex Split & Modify (Simple for Fixed Structure)
If your string structure is consistent (exactly one ","ReserveChar": " separator), this approach avoids regex entirely and is easier to debug.
string originalString = "\"alphabet\": \"dtSearch Alphabet File\\n\\n[Letters] // Original letter, lower case, upper case, unaccented\\n\",\"ReserveChar\": \"{\\\"Datasource\\\":[{\\\"text\\\":\\\"!\\\",\\\"value\\\":\\\"!\\\"},{\\\"text\\\":\\\"^\\\",\\\"value\\\":\\\"^\\\"},{\\\"text\\\":\\\"|\\\",\\\"value\\\":\\\"|\\\"},{\\\"text\\\":\\\"$\\\",\\\"value\\\":\\\"$\\\"}],\\\"macro\\\":\\\"@\\\",\\\"fuzzychar\\\":\\\"%\\\",\\\"stemming\\\":\\\"~\\\",\\\"phonic\\\":\\\"#\\\",\\\"shynonyms\\\":\\\"&\\\",\\\"wightchar\\\":\\\":\\\",\\\"matchdigitchar\\\":\\\"=\\\"}\""; // Split the string into Alphabet part and ReserveChar content string[] parts = originalString.Split(new string[] { "\",\"ReserveChar\": \"" }, StringSplitOptions.None); if (parts.Length == 2) { string alphabetPart = parts[0]; string reserveContent = parts[1]; // Remove the trailing quote from the ReserveChar content reserveContent = reserveContent.TrimEnd('"'); // Unescape all quotes in the ReserveChar content reserveContent = reserveContent.Replace(@"\""", @""""); // Reconstruct the full string string result = $"{alphabetPart}\",\"ReserveChar\": \"{reserveContent}\""; Console.WriteLine(result); } else { // Handle unexpected structure (fallback to original string) Console.WriteLine("String structure does not match expected format."); }
How it works:
- Split the string at the exact separator between
AlphabetandReserveChar. - Isolate the
ReserveCharcontent, unescape its quotes, then reattach it to the originalAlphabetsection. - No risk of modifying
\nin theAlphabetsegment since we never touch that part.
Both solutions will produce the desired result: only the ReserveChar segment's escaped quotes are converted to regular quotes, while the Alphabet section's \n sequences remain intact.
内容的提问来源于stack exchange,提问作者Maharshi
相关产品推荐
相关产品推荐

