Note that there are some explanatory texts on larger screens.

plurals
  1. PO.Net Regex match grouping and repetition question
    primarykey
    data
    text
    <p>I have the following VB.Net 2.0 in an ASP.Net app:</p> <pre><code>output = Regex.Replace(output, "&lt;p&gt;(?:(?:\&lt;\!\-\-.*?\-\-\&gt;)|&amp;(?:nbsp|\#0*160|x0*A0);|&lt;br\s*/?&gt;|[\s\u00A0]+)*&lt;/p&gt;", String.Empty, RegexOptions.Compiled Or RegexOptions.CultureInvariant Or RegexOptions.IgnoreCase Or RegexOptions.Singleline) </code></pre> <p>Example stuff it matches well:</p> <ul> <li><code>&lt;p&gt;&lt;/p&gt;</code></li> <li><code>&lt;p&gt; &lt;/p&gt;</code></li> <li><code>&lt;p&gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;</code></li> <li><code>&lt;p&gt;&lt;!-- comment --&gt;&lt;!-- comment --&gt;&lt;/p&gt;</code></li> <li><code>&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;</code></li> <li><code>&lt;p&gt;&lt;br/&gt;&amp;nbsp;&lt;/p&gt;</code></li> <li><code>&lt;p&gt;&lt;!-- comment --&gt;&lt;br/&gt;&lt;!-- comment --&gt;&lt;/p&gt;</code></li> <li><code>&lt;p&gt;&amp;nbsp;&lt;br/&gt;&lt;/p&gt;</code></li> </ul> <p>Examples of stuff I'd like to match but it doesn't:</p> <ul> <li><code>&lt;p &gt; &lt;!--[if !supportLineBreakNewLine]--&gt;&lt;br /&gt; &lt;!--[endif]--&gt;&lt;/p&gt;</code></li> </ul> <p>How do I make the groups and repetitions work how I want them to?</p> <p><strong>Edit:</strong> oops, forgot the comment group. <strong>Edit #2:</strong> oops, forgot a fail. <strong>Edit #3:</strong> fixed examples. <strong>Edit #4:</strong> updated regex based on answers</p> <p><strong>Conclusion:</strong></p> <p>Here are my benchmarked results for all three answers. Since all three now match everything I ran each one through 10,000 iterations on a block of text:</p> <pre><code>Mine: &lt;p\s*&gt;(?:(?:&lt;!--.*?--&gt;)|&amp;(?:nbsp|\#0*160|x0*A0);|&lt;br\s*/?&gt;|[\s\u00A0]+)*&lt;/p&gt; 6.312 Gumbo: &lt;p\s*&gt;(?:[\s\u00A0]+|&amp;(?:nbsp|\#0*160|x0*A0);|&lt;br\s*/?&gt;|&lt;!--(?:[^-]+|-(?!-))*--&gt;)*&lt;/p&gt; 6.05 steamer25: &lt;p\s*&gt;(?:(?:\&amp;nbsp\;)|(?:\&amp;\#0*160\;)|(?:&lt;br\s*/?&gt;)|\s|\u00A0|&lt;!\-\-[^(?:\-\-)]*\-\-&gt;)*&lt;/p&gt; 6.121 </code></pre> <p>Gumbo's was the fastest, so I'll mark his as the correct answer.</p>
    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.
 

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