I need to insert (single) spaces before and after a specific symbol (e.g. "|"), like this:
string input = "|ABC|xyz |123||999| aaa| |BBB";
string output = "| ABC | xyz | 123 | | 999 | aaa | | BBB";
This can easily be achieved using a couple of regular expression patterns:
string input = "|ABC|xyz |123||999| aaa| |BBB";
// add space before |
string pattern = "[a-zA-Z0-9\\s*]*\\|";
string replacement = "$0 ";
string output = Regex.Replace(input, pattern, replacement);
// add space after |
pattern = "\\|[a-zA-Z0-9\\s*]*";
replacement = " $0";
output = Regex.Replace(output, pattern, replacement);
// trim redundant spaces
pattern = "\\s+";
replacement = " ";
output = Regex.Replace(output, pattern, replacement).Trim();
Console.WriteLine("Original String: \"{0}\"", input);
Console.WriteLine("Replacement String: \"{0}\"", output);
But that is not what I want, my target is just use a single pattern.
I tried many ways but it still doesn't work as expected. Could anybody help me with this please.
Thank you so much in advance!