4

I have the following all number data:

4245 4 0 0242 4424.09 0 422404 5955 0
2234234.234 224 0
2423 234 0

I need to process each line individually. I need to remove all the single 0's and output as follows with commas:

4245,4, 0242,4424.09, 422404,5955
2234234.234,224
2423,234

I got the part of removing the single digits working:

var result = Regex.Replace(inData, @"\b\s0\b", string.Empty);

But cannot figure out how to add the commas in between each number. Any help would be appreciated. Thanks.

Robert Smith
  • 779
  • 1
  • 10
  • 28
  • 1
    Can you not just replace the spaces with commas, like so: result.Replace(" ", ",")? – Bryan Woodford Sep 01 '16 at 14:08
  • Every two numbers I also need an extra space. I thought it would be possible with regex expression but maybe not. For example 1 2 0 5 6 0 should produce 1,2 5,6 – Robert Smith Sep 01 '16 at 14:30

4 Answers4

2

You can achieve what you want with one Regex.Replace operation, but with a custom match evaluator:

var input = "4245 4 0 242 4424.09 0 422404 5955 0";
var results = Regex.Replace(input, @"(?:\s+|^)0(\s+)|(\s+0)$|\s+", m =>
           m.Groups[1].Success ? ", " :
           m.Groups[2].Success ? "" : ",");

enter image description here

The point is to match those parts we need and capture into groups, so that they can be further analyzed and an appropriate action could be taken.

Pattern details:

  • (?:\s+|^)0(\s+) - match 0 that is either at the start or with whitespaces before it and that is followed with 1 or more whitespaces (the whitespaces after 0 are stored in Group 1)
  • | - or
  • (\s+0)$ - Group 2 capturing one or more whitespaces, then a 0 at the end ($) of the string
  • | - or
  • \s+ - (3rd option) 1 or more whitespaces in all other contexts.

And just in case one likes a more readable version, here is an alternative where the final 0 is removed with string methods, and then 1 regex is used to replace all spaces inside digits with a comma, but before we replace all 0 s with a mere String.Replace.

var inp = "4245 4 0 0242 4424.09 0 422404 5955 0";
inp = inp.EndsWith(" 0") ? inp.Substring(0, inp.Length - 2) : inp;
var output = Regex.Replace(inp.Replace(" 0 ", ", "), @"(\d) (\d)", "$1,$2");
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Wow thank you for the answer and explanation! This works perfect Wiktor. – Robert Smith Sep 01 '16 at 14:41
  • Wiktor, quick question. What would it take to also remove -0. For example "2423 234 -0 1 2 0" would output "2423,234 1,2" Thanks. – Robert Smith Sep 01 '16 at 20:15
  • It is easy, just add an optional `-`: `@"(?:\s+|^)-?0(\s+)|(\s+-?0)$|\s+"` – Wiktor Stribiżew Sep 01 '16 at 20:28
  • Wiktor thanks for your help. If you by any chance you have a spare moment, can you please take a look at this one: http://stackoverflow.com/questions/39472897/regex-to-remove-the-third-number-and-add-commas Thanks. – Robert Smith Sep 13 '16 at 15:23
1

I understand that you want to

  • Replace spaces with commas ("xy z" => "xy,z")
  • Replace single zeros with spaces ("xy 0 z" => "xy, z")

Then I would recommend two string replacements:

  1. inData.replace(" ", ",");,
  2. inData.replace(",0", " ");
vauhochzett
  • 2,732
  • 2
  • 17
  • 40
1

Using this will replace any whitespace character with a comma.

var result = Regex.Replace(inData, @"\s+", ",");

\s+ matches any whitespace character.

Then run your other regex to remove the single digit 0's

DNKROZ
  • 2,634
  • 4
  • 25
  • 43
  • Thank you! Can I do everything with one regex? – Robert Smith Sep 01 '16 at 14:29
  • Sorry at first i didn't see that the 0's were just removed and not changed into commas! You will need to either use 2 regex functions or 2 string replace functions – DNKROZ Sep 01 '16 at 14:30
  • Thanks. Is there any way to do this in one regex? For example 1 2 0 5 6 0 should produce 1,2 5,6 – Robert Smith Sep 01 '16 at 14:32
  • Yes - check out Wiktor's answer – DNKROZ Sep 01 '16 at 14:36
  • @DNKROZ: Your pattern should be written as `@"\s+"`, there should be no regex delimiters in .NET regex patterns. `Regex.Replace` performs a global search and replace by default (and in general, only an object of Regex class has a `Replace` overload to accept a replacement limit argument). – Wiktor Stribiżew Sep 01 '16 at 14:38
  • 1
    Thanks @WiktorStribiżew. I originally posted without delimiters but added them in as i thought I'd missed them out! Thanks for the information though - did not realise that they were included by default – DNKROZ Sep 01 '16 at 14:42
  • 1
    Thank you for the explanation DNKROZ, great job. – Robert Smith Sep 01 '16 at 14:42
  • 1
    @DNKROZ: You are welcome :) See also http://stackoverflow.com/questions/31560080/removing-all-non-word-characters-with-regex – Wiktor Stribiżew Sep 01 '16 at 14:45
0

You could just do a string.replace(" ", ","), right? (if I am understanding your question correctly)

Or you could even do a string.split(" ") into an array, then string.join(','). Although this is probably less efficient.

Ian656
  • 1
  • 2
  • Every two numbers I also need an extra space. Not sure if that would work. I thought it would be possible with regex expression but maybe not. For example 1 2 0 5 6 0 should produce 1,2 5,6 – Robert Smith Sep 01 '16 at 14:23
  • You could do that with the array created from the split (as long as you aren't working with extremely large numbers). Otherwise you could use the implementation that vauhochzett mentioned, and afterwords pattern match for the ",d*," and replace the first comma with blank space. – Ian656 Sep 01 '16 at 15:09