Replace commas in single quotes but ignore comma separated string RRS feed

  • Question

  • Hello,

    I would like to use regular expression to replace commas in single quotes with spaces, but ignore comma separated string.

    Does anyone know how to do in C#?


    Original string:

    "abc,'this,demo',foo,'hello,world', bar, ,"

    After replacing the commas in quotes, the result is:

    "abc,'this demo',foo,'hello world', bar, ,"

    Thursday, January 11, 2018 7:07 PM

All replies

  • Check this:

    var example = "abc,'this,demo',foo,'hello,world', bar, ,";
    var result = Regex.Replace( example, @",(?=[^']*'([^']*'[^']*')*[^']*$)", " " );
    Console.WriteLine( example );
    Console.WriteLine( result );

    Thursday, January 11, 2018 8:22 PM
  • Thank you so much
    Friday, January 12, 2018 9:38 PM
  • Hi psarr,

    Based on my test, Viorel_'s could achieve your requirement, if the reply resolve your issue, could you please mark it as answer, it will be beneficial to other communities who have the similar issue.

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, January 17, 2018 8:09 AM