<p>How can I replace lone instances of \n with \r\n (LF alone with CRLF) using a regular expression in C#?</p>
<p>I know to do it using plan <code>String.Replace</code>, like:</p>
<pre><code>myStr.Replace(&quot;\n&quot;, &quot;\r\n&quot;);
myStr.Replace(&quot;\r\r\n&quot;, &quot;\r\n&quot;);
</code></pre>
<p>However, this is inelegant, and would destroy any &quot;\r+\r\n&quot; already in the text (although they are not likely to exist).</p>

[{'answer_id': 31056, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 7, 'selected': True, 'text': '<p>Will this do?</p>\n\n<pre><code>[^\\r]\\n\n</code></pre>\n\n<p>Basically it matches a \'\\n\' that is preceded with a character that is not \'\\r\'.</p>\n\n<p>If you want it to detect lines that start with just a single \'\\n\' as well, then try</p>\n\n<pre><code>([^\\r]|$)\\n\n</code></pre>\n\n<p>Which says that it should match a \'\\n\' but only those that is the first character of a line or those that are <em>not</em> preceded with \'\\r\'</p>\n\n<p>There might be special cases to check since you\'re messing with the definition of lines itself the \'$\' might not work too well. But I think you should get the idea.</p>\n\n<p><strong>EDIT:</strong> credit @Kibbee Using look-ahead s is clearly better since it won\'t capture the matched preceding character and should help with any edge cases as well. So here\'s a better regex + the code becomes:</p>\n\n<pre><code>myStr = Regex.Replace(myStr, "(?&lt;!\\r)\\n", "\\r\\n");\n</code></pre>\n'}, {'answer_id': 31070, 'author': 'neslekkiM', 'author_id': 3308, 'author_profile': 'https://Stackoverflow.com/users/3308', 'pm_score': 2, 'selected': False, 'text': '<p>I guess that "myStr" is an object of type String, in that case, this is not regex.\n\\r and \\n are the equivalents for CR and LF.</p>\n\n<p>My best guess is that if you know that you have an \\n for EACH line, no matter what, then you first should strip out every \\r. Then replace all \\n with \\r\\n.</p>\n\n<p>The answer chakrit gives would also go, but then you need to use regex, but since you don\'t say what "myStr" is...</p>\n\n<p>Edit:looking at the other examples tells me one thing.. why do the difficult things, when you can do it easy?, Because there is regex, is not the same as "must use" :D</p>\n\n<p>Edit2: A tool is very valuable when fiddling with regex, xpath, and whatnot that gives you strange results, may I point you to: <a href="http://www.regexbuddy.com/" rel="nofollow noreferrer">http://www.regexbuddy.com/</a> </p>\n'}, {'answer_id': 31074, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 1, 'selected': False, 'text': '<pre><code>myStr.Replace("([^\\r])\\n", "$1\\r\\n");\n</code></pre>\n\n<p>$ may need to be a \\</p>\n'}, {'answer_id': 32704, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 6, 'selected': False, 'text': '<p>It might be faster if you use this.</p>\n\n<pre><code>(?&lt;!\\r)\\n\n</code></pre>\n\n<p>It basically looks for any \\n that is not preceded by a \\r.  This would most likely be faster, because in the other case, almost every letter matches [^\\r], so it would capture that,  and then look for the \\n after that.  In the example I gave, it would only stop when it found a \\n, and them look before that to see if it found \\r</p>\n'}, {'answer_id': 6449289, 'author': 'wanted', 'author_id': 811500, 'author_profile': 'https://Stackoverflow.com/users/811500', 'pm_score': 4, 'selected': False, 'text': '<p>I was trying to do the code below to a string and it was not working.</p>\n\n<pre><code>myStr.Replace("(?&lt;!\\r)\\n", "\\r\\n")\n</code></pre>\n\n<p>I used Regex.Replace and it worked</p>\n\n<pre><code>Regex.Replace( oldValue, "(?&lt;!\\r)\\n", "\\r\\n")\n</code></pre>\n'}, {'answer_id': 6951416, 'author': 'Ico', 'author_id': 879855, 'author_profile': 'https://Stackoverflow.com/users/879855', 'pm_score': 1, 'selected': False, 'text': '<p>Try this: Replace(Char.ConvertFromUtf32(13), Char.ConvertFromUtf32(10) + Char.ConvertFromUtf32(13)) </p>\n'}, {'answer_id': 24972759, 'author': 'Kevin Jin', 'author_id': 444402, 'author_profile': 'https://Stackoverflow.com/users/444402', 'pm_score': 1, 'selected': False, 'text': '<p>If I know the line endings must be one of CRLF or LF, something that works for me is</p>\n\n<pre><code>myStr.Replace("\\r?\\n", "\\r\\n");\n</code></pre>\n\n<p>This essentially does the same <a href="https://stackoverflow.com/a/31070/444402">neslekkiM</a>\'s answer except it performs only one replace operation on the string rather than two. This is also compatible with Regex engines that don\'t support negative lookbehinds or backreferences.</p>\n'}]
['https://Stackoverflow.com/questions/31053', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/838/']