<p>I have a big string (let's call it a CSV file, though it isn't actually one, it'll just be easier for now) that I have to parse in C# code.<br>
The first step of the parsing process splits the file into individual lines by just using a <code>StreamReader</code> object and calling <code>ReadLine</code> until it's through the file. However, any given line might contain a quoted (in single quotes) literal with embedded newlines.  I need to find those newlines and convert them temporarily into some other kind of token or escape sequence until I've split the file into an array of lines..then I can change them back. </p>

<p>Example input data: </p>

<pre><code>1,2,10,99,'Some text without a newline', true, false, 90
2,1,11,98,'This text has an embedded newline 
                and continues here', true, true, 90
</code></pre>

<p>I could write all of the C# code needed to do this by using <code>string.IndexOf</code> to find the quoted sections and look within them for newlines, but I'm thinking a Regex might be a better choice (i.e. <a href="http://regex.info/blog/2006-09-15/247" rel="nofollow noreferrer">now I have two problems</a>)</p>

[{'answer_id': 33074, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 0, 'selected': False, 'text': '<p><strong>EDIT:</strong> Sorry, I\'ve misinterpreted your post. If you\'re looking for a regex, then here is one:</p>\n\n<pre><code>content = Regex.Replace(content, "\'([^\']*)\\n([^\']*)\'", "\'\\1TOKEN\\2\'");\n</code></pre>\n\n<p>There might be edge cases and that two problems but I think it should be ok most of the time. What the Regex does is that it first finds any pair of single quotes that has \\n between it and replace that \\n with TOKEN preserving any text in-between.</p>\n\n<p>But still, I\'d go state machine like what @bryansh explained below.</p>\n'}, {'answer_id': 33075, 'author': 'EBGreen', 'author_id': 1358, 'author_profile': 'https://Stackoverflow.com/users/1358', 'pm_score': 1, 'selected': False, 'text': '<p>What if you got the whole file into a variable then split that based on non-quoted newlines?</p>\n'}, {'answer_id': 33112, 'author': 'bryansh', 'author_id': 211367, 'author_profile': 'https://Stackoverflow.com/users/211367', 'pm_score': 3, 'selected': True, 'text': "<p>Since this isn't a true CSV file, does it have any sort of schema?</p>\n\n<p>From your example, it looks like you have:\nint, int, int, int, string , bool, bool, int</p>\n\n<p>With that making up your record / object.</p>\n\n<p>Assuming that your data is well formed (I don't know enough about your source to know how valid this assumption is); you could:</p>\n\n<ol>\n<li>Read your line.</li>\n<li>Use a state machine to parse your data.</li>\n<li>If your line ends, and you're parsing a string, read the next line..and keep parsing.</li>\n</ol>\n\n<p>I'd avoid using a regex if possible.</p>\n"}, {'answer_id': 33172, 'author': 'Duncan Smart', 'author_id': 1278, 'author_profile': 'https://Stackoverflow.com/users/1278', 'pm_score': 2, 'selected': False, 'text': '<p>State-machines for doing such a job are made easy using C# 2.0 iterators. Here\'s hopefully the last CSV parser I\'ll ever write. The whole file is treated as a enumerable bunch of enumerable strings, i.e. rows/columns. IEnumerable is great because it can then be processed by LINQ operators.</p>\n\n<pre><code>public class CsvParser\n{\n    public char FieldDelimiter { get; set; }\n\n    public CsvParser()\n        : this(\',\')\n    {\n    }\n\n    public CsvParser(char fieldDelimiter)\n    {\n        FieldDelimiter = fieldDelimiter;\n    }\n\n    public IEnumerable&lt;IEnumerable&lt;string&gt;&gt; Parse(string text)\n    {\n        return Parse(new StringReader(text));\n    }\n    public IEnumerable&lt;IEnumerable&lt;string&gt;&gt; Parse(TextReader reader)\n    {\n        while (reader.Peek() != -1)\n            yield return parseLine(reader);\n    }\n\n    IEnumerable&lt;string&gt; parseLine(TextReader reader)\n    {\n        bool insideQuotes = false;\n        StringBuilder item = new StringBuilder();\n\n        while (reader.Peek() != -1)\n        {\n            char ch = (char)reader.Read();\n            char? nextCh = reader.Peek() &gt; -1 ? (char)reader.Peek() : (char?)null;\n\n            if (!insideQuotes &amp;&amp; ch == FieldDelimiter)\n            {\n                yield return item.ToString();\n                item.Length = 0;\n            }\n            else if (!insideQuotes &amp;&amp; ch == \'\\r\' &amp;&amp; nextCh == \'\\n\') //CRLF\n            {\n                reader.Read(); // skip LF\n                break;\n            }\n            else if (!insideQuotes &amp;&amp; ch == \'\\n\') //LF for *nix-style line endings\n                break;\n            else if (ch == \'"\' &amp;&amp; nextCh == \'"\') // escaped quotes ""\n            {\n                item.Append(\'"\');\n                reader.Read(); // skip next "\n            }\n            else if (ch == \'"\')\n                insideQuotes = !insideQuotes;\n            else\n                item.Append(ch);\n        }\n        // last one\n        yield return item.ToString();\n    }\n\n}\n</code></pre>\n\n<p>Note that the file is read character by character with the code deciding when newlines are to be treated as row delimiters or part of a quoted string.</p>\n'}]
['https://Stackoverflow.com/questions/33063', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2187/']