Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there is an escape out of the code mode with any trailing newline not followed by tab or 4 spaces. Not sure but successive newlines would not be included in the code block.</p> <p>I don't get Ruby's regex options too well, but this seems to work: <a href="http://rubular.com/r/BlbreoO3sn" rel="nofollow">http://rubular.com/r/BlbreoO3sn</a></p> <p><code>((?:^(?:[ ]{4}|\t).*$(?:\r?\n|\z))+)</code> Theorhetically, its in multi-line mode. </p> <p>Just make the replacement <code>&lt;pre&gt;\1&lt;/pre&gt;</code></p> <p><strong>EDIT</strong><br> @Rachela Meadows - After further examination, this is a fairly difficult regex.<br> I managed to exactly duplicate the functionality of the <code>&lt;pre&gt;&lt;code&gt;</code> block features of the online editor here on SO.</p> <p>After obtaining each block and before wrapping in a <code>&lt;pre&gt;&lt;code&gt;</code>, all markup entities should be converted (ie; like <code>&lt;</code> to <code>&amp;lt;</code>, etc). That being said, I didn't do that step in the Ruby code sample below. I do have the regex's to do that though.</p> <p>A special note about trimming: The main regex below does not include residual trailing newlines. Nor does the SO functionality. So the code block is correct top to bottom.<br> However, the leading 4 spaces (or tab) that <em>could</em> be contained in the body can't be trimmed (and they should be) in the main regex. For that it needs a callback.</p> <p>Playing around with the <code>gsub</code> <em>block</em> mode, its easy to trim those leading spaces/tab.</p> <p>Let me know if you have any problems with this.</p> <p>Links -<br> Rubular (for the regex): <a href="http://rubular.com/r/pp9oRLQ0xo" rel="nofollow">http://rubular.com/r/pp9oRLQ0xo</a><br> Ideone (for the working Ruby code): <a href="http://ideone.com/aA9it" rel="nofollow">http://ideone.com/aA9it</a></p> <p>Regex compressed -<br> <code>(^\s*$\n|\A)(^(?:[ ]{4}|\t).*[^\s].*$\n?(?:(?:^\s*$\n?)*^(?:[ ]{4}|\t).*[^\s].*$\n?)*)</code> </p> <p>Regex expanded - </p> <pre><code>(^\s*$\n|\A) # Capt grp 1, block is preceeded by a blank line or begin of string ( # Begin "Capture group 2", start of pre/code block ^(?:[ ]{4}|\t) .* [^\s] .* $ \n? # First line of code block (note - lines must contain at least 1 non-whitespace character) (?: # Start "Optionally, get more lines of code" (?: ^ \s* $ \n? )* # Many optional blank lines ^(?:[ ]{4}|\t) .* [^\s] .* $ \n? # Another line of code )* # End "Optionally, get more lines of code", do 0 or more times ) # End "Capture group 2", end of pre/code block </code></pre> <p>Ruby code - </p> <pre><code> regex = /(^\s*$\n|\A)(^(?:[ ]{4}|\t).*[^\s].*$\n?(?:(?:^\s*$\n?)*^(?:[ ]{4}|\t).*[^\s].*$\n?)*)/; data = ' Hello Worldsasdasdffasdfasdf asdf thisdqweee asdfasdfasdfasdf sdfg #YYYY { height: 100%; min-height: 800px; margin-right: 20px; position: relative; } #ZZZZZZ { height: 100%; overflow: hidden; }'; # --- result = data.gsub(regex) { || x=$2; ## Construct the return value '\1&lt;pre&gt&lt;code&gt\2&lt;/code&gt&lt;/pre&gt'. ## But, trim each line with 1 to 4 leading spaces (or a tab with regex on the bottom). ## They are not necessary now, they are replaced with a code block. $1 + '&lt;pre&gt&lt;code&gt' + x.gsub(/^[ ]{1,4}/, '') + '&lt;/code&gt&lt;/pre&gt' }; # Note - Tabs can be trimed too, use : x.gsub(/^(?:[ ]{1,4}|\t)/,'') in the above print result; </code></pre>
    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. 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