0

I have a string as in the following format:

"one,",2,3,"four " ","five"

I need output in the following format:

one,
2
3
four " 
five

Can anyone help me to create Regex for the above?

Omar
  • 16,329
  • 10
  • 48
  • 66
  • Why do you want a RegEx? This is trivial to achieve with `String.Split` and `Trim`. – Rawling Sep 11 '12 at 09:56
  • What's wrong with String.Replace("\"", " ") and String.Split(',')? – t3hn00b Sep 11 '12 at 09:57
  • It looks like CSV data. What do you expect for this: "one,two",2,3? You may want to have a look at a CSV reader/parser: http://stackoverflow.com/questions/906841/csv-parser-reader-for-c – Martin Sep 11 '12 at 09:58
  • +1 cause your question is good and this is what the site is for. – ParPar Sep 11 '12 at 10:06

2 Answers2

1

You can do this without Regex. It's not clear to me, what you're trying to do though. I've adjusted the code for the updated question:

var text = "\"one\",2,3,\"four \"\",\"five\"";

var collection = text
    .Split(',')
    .Select(s =>
                {
                    if (s.StartsWith("\"") && s.EndsWith("\""))
                    {
                        s = s.Substring(1, s.Length - 2);
                    }
                    return s;
                })
    .ToList();

foreach (var item in collection)
{
    Console.WriteLine(item);
}

I've added another sample for you, which uses a CSV reader. I've installed the "CsvHelper" package from NuGet:

const string text = "\"one,\",2,3,\"four \"\"\",\"five\"";

using (var textReader = new StringReader(text))
using (var reader = new CsvReader(textReader))
{
    reader.Configuration.Delimiter = ',';
    reader.Configuration.AllowComments = false;
    reader.Configuration.HasHeaderRecord = false;

    if (reader.Read())
    {
        foreach (var item in reader.CurrentRecord)
        {
            Console.WriteLine(item);
        }
    }
}
Martin
  • 2,302
  • 2
  • 30
  • 42
  • There might be a , within the string. for example "one," in the above example. – user1662450 Sep 11 '12 at 10:14
  • Thanks for the update but the first approach will not work with the additional comma(,) which is there for "one," and the second approach will not work for the additional quotation which is there for "four " ". – user1662450 Sep 14 '12 at 07:37
  • The second approach uses CSV. From Wikipedia: "Fields with embedded double-quote characters must be quoted, and each of the embedded double-quote characters must be represented by a pair of double-quote characters.". You can read more about the CSV format here: http://en.wikipedia.org/wiki/Comma-separated_values. I've also updated the sample for you, so you can see how to add the extra ". – Martin Sep 14 '12 at 11:47
0
  string newString = Regex.Replace(oldString, @'[^",]', ' ');

I hope the regular expression is good, but I just want you you to see the idea.

EDIT:

 string newString = Regex.Replace(oldString, @'[^",]', '\n');
ParPar
  • 7,355
  • 7
  • 43
  • 56