3

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!

Phong Ho
  • 31
  • 3
  • Define "doesn't work as expected". Do you get nothing back? The wrong result? An error? – Tim Oct 05 '16 at 03:34
  • No error but wrong output result, for example there are more spaces than necessary (instead of 1 space), that's why I thought about combining a couple of patterns, but is it possible to use just a single pattern to achieve this? – Phong Ho Oct 05 '16 at 03:41

3 Answers3

4

Thanks @Santhosh Nayak.

I just write more C# code to get the output as OP want.

string input = "|ABC|xyz |123||999|   aaa|   |BBB";
string pattern = @"[\s]*[|][\s]*";
string replacement = " | ";
string output = Regex.Replace(input, pattern, (match) => {
     if(match.Index != 0)
        return replacement;
     else
        return value;
});

I refer to Regex.Replace(string input, string pattern, MatchEvaluator evaluator) in MSDN.

cat916
  • 1,363
  • 10
  • 18
0

Try This.

string input = "|ABC|xyz |123||999|   aaa|   |BBB";

string pattern = @"[\s]*[|][\s]*";
string replacement = " | ";
string output = Regex.Replace(input, pattern, replacement);
Santhosh Nayak
  • 2,312
  • 3
  • 35
  • 65
  • Thanks, but there are still redundant white spaces between `"|"`, output is `" | ABC | xyz | 123 | | 999 | aaa | | BBB"`. If there is no solution, perhaps I have to use one more pattern to remove redundant white spaces. – Phong Ho Oct 05 '16 at 04:14
  • @PhongHo I think you can use his regex pattern to get your result. We just write more Regex C# code :) . – cat916 Oct 08 '16 at 04:58
0

Try this solution, based on this answer:

var str = "|ABC|xyz |123||999|   aaa|   |BBB";
var fixed = Regex.Replace(str, patt, m =>
            {
                if(string.IsNullOrWhiteSpace(m.Value))//multple spaces
                    return "";
                return " | ";
            });

This returns | ABC | xyz | 123 | | 999 | aaa | | BBB

We stil have |(space)(space)| between aaa and BBB but that is due to replacement of | with |.

Community
  • 1
  • 1
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188