Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the perfect Regex for doing wiki formatting (.Net)?
    primarykey
    data
    text
    <p>H guys, I've got this wiki formatting algorithm which I am using at <a href="http://stacked.ra-ajax.org/" rel="nofollow noreferrer">Stacked</a> to create HTML out of "wiki syntax" and I am not really sure if the current one I am using is good enough, optimal or contains bugs since I am not really a "Regex Guru". Here is what I am currently using;</p> <pre><code>// Body is wiki content... string tmp = Body.Replace("&amp;", "&amp;amp;").Replace("&lt;", "&amp;lt;").Replace("&gt;", "&amp;gt;"); // Sanitizing carriage returns... tmp = tmp.Replace("\\r\\n", "\\n"); // Replacing dummy links... tmp = Regex.Replace( " " + tmp, "(?&lt;spaceChar&gt;\\s+)(?&lt;linkType&gt;http://|https://)(?&lt;link&gt;\\S+)", "${spaceChar}&lt;a href=\"${linkType}${link}\"" + nofollow + "&gt;${link}&lt;/a&gt;", RegexOptions.Compiled).Trim(); // Replacing wiki links tmp = Regex.Replace(tmp, "(?&lt;begin&gt;\\[{1})(?&lt;linkType&gt;http://|https://)(?&lt;link&gt;\\S+)\\s+(?&lt;content&gt;[^\\]]+)(?&lt;end&gt;[\\]]{1})", "&lt;a href=\"${linkType}${link}\"" + nofollow + "&gt;${content}&lt;/a&gt;", RegexOptions.Compiled); // Replacing bolds tmp = Regex.Replace(tmp, "(?&lt;begin&gt;\\*{1})(?&lt;content&gt;.+?)(?&lt;end&gt;\\*{1})", "&lt;strong&gt;${content}&lt;/strong&gt;", RegexOptions.Compiled); // Replacing italics tmp = Regex.Replace(tmp, "(?&lt;begin&gt;_{1})(?&lt;content&gt;.+?)(?&lt;end&gt;_{1})", "&lt;em&gt;${content}&lt;/em&gt;", RegexOptions.Compiled); // Replacing lists tmp = Regex.Replace(tmp, "(?&lt;begin&gt;\\*{1}[ ]{1})(?&lt;content&gt;.+)(?&lt;end&gt;[^*])", "&lt;li&gt;${content}&lt;/li&gt;", RegexOptions.Compiled); tmp = Regex.Replace(tmp, "(?&lt;content&gt;\\&lt;li\\&gt;{1}.+\\&lt;\\/li\\&gt;)", "&lt;ul&gt;${content}&lt;/ul&gt;", RegexOptions.Compiled); // Quoting tmp = Regex.Replace(tmp, "(?&lt;content&gt;^&amp;gt;.+$)", "&lt;blockquote&gt;${content}&lt;/blockquote&gt;", RegexOptions.Compiled | RegexOptions.Multiline).Replace("&lt;/blockquote&gt;\n&lt;blockquote&gt;", "\n"); // Paragraphs tmp = Regex.Replace(tmp, "(?&lt;content&gt;)\\n{2}", "${content}&lt;/p&gt;&lt;p&gt;", RegexOptions.Compiled); // Breaks tmp = Regex.Replace(tmp, "(?&lt;content&gt;)\\n{1}", "${content}&lt;br /&gt;", RegexOptions.Compiled); // Code tmp = Regex.Replace(tmp, "(?&lt;begin&gt;\\[code\\])(?&lt;content&gt;[^$]+)(?&lt;end&gt;\\[/code\\])", "&lt;pre class=\"code\"&gt;${content}&lt;/pre&gt;", RegexOptions.Compiled); // Now hopefully tmp will contain perfect HTML </code></pre> <p>For those who thinks it's difficult to see the code here, you can also check it out <a href="http://code.google.com/p/stacked/source/browse/trunk/WebSite/App_Code/QuizItem.cs" rel="nofollow noreferrer">here</a>...</p> <p>Here is the complete "wiki syntax";</p> <p>Syntax here:</p> <pre><code>Link; [http://x.com text] *bold* (asterisk on both sides) _italic_ (underscores on both sides) * Listitem 1 * Listitem 2 * Listitem 3 (the above is asterixes but so.com also creates lists from it) 2 x Carriage Return is opening a new paragraph 1 x Carriage Return is break (br) [code] if( YouDoThis ) YouCanWriteCode(); [/code] &gt; quote (less then operator) </code></pre> <p>If there are some "Regex gurus" who would like to review this Regex logic I'd appreciate it a lot :)</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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