Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alright this answer is in accordance to the clarified requirements specified in the bounty:</p> <blockquote> <p>I also need to remove any trailing newlines, and my Regex-fu is failing. My bounty goes to anyone who can give me a regex which passes this test: StripWhitespace("test\r\n \r\nthis\r\n\r\n") == "test\r\nthis"</p> </blockquote> <p>So Here's the answer:</p> <pre><code>(?&lt;=\r?\n)(\s*$\r?\n)+|(?&lt;=\r?\n)(\r?\n)+|(\r?\n)+\z </code></pre> <p>Or in the C# code provided by @Chris Schmich:</p> <pre><code>string fix = Regex.Replace("test\r\n \r\nthis\r\n\r\n", @"(?&lt;=\r?\n)(\s*$\r?\n)+|(?&lt;=\r?\n)(\r?\n)+|(\r?\n)+\z", string.Empty, RegexOptions.Multiline); </code></pre> <p>Now let's try to understand it. There are three optional patterns in here which I am willing to replace with <code>string.empty</code>.</p> <ol> <li><code>(?&lt;=\r?\n)(\s*$\r?\n)+</code> - matches one to unlimited lines containing only white space and preceeded by a line break (but does not match the first preceeding line breaks).</li> <li><code>(?&lt;=\r?\n)(\r?\n)+</code> - matches one to unlimited empty lines with no content that are preceeded by a line break (but does not match the first preceeding line breaks).</li> <li><code>(\r?\n)+\z</code> - matches one to unlimited line breaks at the end of the tested string (trailing line breaks as you called them)</li> </ol> <p>That satisfies your test perfectly! But also satisfies both <code>\r\n</code> and <code>\n</code> line break styles! Test it out! I believe this will be the most correct answer, although simpler expression would pass your specified bounty test, this regex passes more complex conditions.</p> <p><strong>EDIT:</strong> @Will pointed out a potential flaw in the last pattern match of the above regex in that it won't match multiple line breaks containing white space at the end of the test string. So let's change that last pattern to this: </p> <p><code>\b\s+\z</code> The \b is a word boundry (beginning or END of a word), the \s+ is one or more white space characters, the \z is the end of the test string (end of "file"). So now it will match any assortment of whitespace at the end of the file including tabs and spaces in addition to carriage returns and line breaks. I tested both of @Will's provided test cases. </p> <p>So all together now, it should be:</p> <pre><code>(?&lt;=\r?\n)(\s*$\r?\n)+|(?&lt;=\r?\n)(\r?\n)+|\b\s+\z </code></pre> <p><strong>EDIT #2:</strong> Alright there is one more possible case @Wil found that the last regex doesn't cover. That case is inputs that have line breaks at the beginning of the file before any content. So lets add one more pattern to match the beginning of the file.</p> <p><code>\A\s+</code> - The <code>\A</code> match the beginning of the file, the <code>\s+</code> match one or more white space characters.</p> <p>So now we've got:</p> <pre><code>\A\s+|(?&lt;=\r?\n)(\s*$\r?\n)+|(?&lt;=\r?\n)(\r?\n)+|\b\s+\z </code></pre> <p>So now we have four patterns for matching:</p> <ol> <li>whitespace at the beginning of the file,</li> <li>redundant line breaks containing white space, (ex: <code>\r\n \r\n\t\r\n</code>)</li> <li>redundant line breaks with no content, (ex: <code>\r\n\r\n</code>)</li> <li>whitespace at the end of the file</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload