8

I have a string similar to this one:

The boy said to his mother, "Can I have some candy?"

If I do a normal String.Split on it, I get:

{ 'The', 'boy', 'said', 'to', 'his', 'mother', '"Can', 'I', 'have', 'some', 'candy?"' }

I want an array like so:

{ 'The', 'boy', 'said', 'to', 'his', 'mother', 'Can I have some candy?' }

Obviously, I could just loop through character by character and keep track of whether I'm in a string or not and all that... but is there a better way? With Regexs perhaps?

1
  • consider "parse string like shell" as a useful phrase for this sort of ask Commented Jun 4, 2011 at 22:53

2 Answers 2

9

How about finding all the matches of this regex:

"[^"]*"|\S+
Sign up to request clarification or add additional context in comments.

2 Comments

This is great! One more thing though... is it possible to remove the quotes from the "Can I have some candy?" match?
@TheAdamGaskins: String.Trim('"').
2

Depends a bit on your requirements. E.g. do you need to treat AAA"BBB (no spaces) as signle word, or two words? If AAA"BBB is a single word, and " only starts a qouted field after delimiter - this looks like CSV parser. Of course, CSV has other rules, like double qoutes to mean literal quote, etc - but you would need to define some similar rules too.

So you can adapt any open source CSV parser, or see if e.g. Microsoft.VisualBasic.FileIO.TextFieldParser works for you

        string msg = "The boy said to his mother, \"Can I have some candy?\"";
        System.IO.MemoryStream s = new System.IO.MemoryStream(Encoding.Unicode.GetBytes(msg));
        TextFieldParser p = new TextFieldParser(s, Encoding.Unicode);
        p.Delimiters = new string[] { " ", "," };
        foreach(var f in p.ReadFields().Where(f => f != ""))
            Console.WriteLine(f);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.