Mayou
Mayou

Reputation: 8818

Replacing a pattern in C#

Consider the following string:

##snapshot

string s = "a,,,b,c,,,,d";

###

I am trying to get the following result:

"a,null,null,b,c,null,null,null,d";

In other words, if there is nothing between two commas, I would like to replace that by "null". I tried the following:

using System.Text.RegularExpressions;     

### snapshot

Regex r = new Regex("[\\,\\,]");
Console.WriteLine(r.Replace(s, ",null,"));

and I get:

enter image description here

I don't know much about regular expressions, and this is the best I could come up with, but the result is wrong. What can I try next?

Upvotes: 0

Views: 118

Answers (9)

user557597
user557597

Reputation:

If going to be like a csv but you need fill empty col's you could use a lookbehind and lookahead.

find: (?<=^|,)(?=,|$)
replace "null"

Or, if whitespaces involved between commas
find: (?<=^|,)\s*(?=,|$)
replace "null"

Upvotes: 2

David
David

Reputation: 6571

using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System;

 public class Example
{
   public static void Main()
   {
        string str = @"a,,,b,c,,,,d";
        Regex pattern = new Regex(@",,");
        string replacement = @",null,";
        Match m = pattern.Match(str);
        while (m.Success)
        {
            str = pattern.Replace(str, replacement);
            m = pattern.Match(str);
        }

        Console.WriteLine(str);
    }
}

Upvotes: 0

Hogan
Hogan

Reputation: 70513

Perhaps more intuitive than some answers -- keep checking to see if there is something to replace till done. (Because you use the "results" of the each match):

  string s = "a,,,b,c,,,,d";

  while (Regex.Match(s,"(,,)").Success == true)
    s =  Regex.Replace(s,"(,,)",",null,");

Upvotes: 1

Bart Friederichs
Bart Friederichs

Reputation: 33511

The [] operator in regexes mean "any of these". Remove them, you want to match against ,, literally.

Or just use Replace, no need for regular expressions.

Upvotes: 1

Zeus
Zeus

Reputation: 6566

/\,(?=\,)/g

use this for positive look ahead and replace it using c# code. with ',null'

Upvotes: 1

Austin Salonen
Austin Salonen

Reputation: 50215

If you're allowed other options, you just just split and rejoin the processed values like so:

string s = "a,,,b,c,,,,d";
var cleaned = s.Split(',').Select(x => string.IsNullOrEmpty(x) ? "null" : x);
var result = string.Join(",", cleaned);

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

One approach would be like this:

class Program
{
    static void Main(string[] args)
    {
        string s = "a,,,b,c,,,,d";
        Console.WriteLine(string.Join(",",
            s.Split(',')
            .Select(c => string.IsNullOrEmpty(c) ? "null" : c)));
    }
}

Upvotes: 0

vcsjones
vcsjones

Reputation: 141598

Use a positive look ahead:

string s = "a,,,b,c,,,,d";
var replaced = Regex.Replace(s, ",(?=,)", ",null");
Console.WriteLine(replaced);

Upvotes: 5

L.B
L.B

Reputation: 116108

You don't need regex for this operation

string s = "a,,,b,c,,,,d";
var str = String.Join(",", s.Split(',')
                            .Select(x => String.IsNullOrEmpty(x) ? "null" : x));

Upvotes: 2

Related Questions