Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you just want to remove all the tags from the string, use this (C#):</p> <pre><code>try { yourstring = Regex.Replace(yourstring, "(&lt;[be]pt[^&gt;]+&gt;.+?&lt;/[be]pt&gt;)", ""); } catch (ArgumentException ex) { // Syntax error in the regular expression } </code></pre> <p>EDIT:</p> <p>I decided to add on to my solution with a better option. The previous option would not work if there were embedded tags. This new solution should strip all &lt;**pt*> tags, embedded or not. In addition, this solution uses a back reference to the original [be] match so that the exact matching end tag is found. This solution also creates a reusable Regex object for improved performance so that each iteration does not have to recompile the Regex:</p> <pre><code>bool FoundMatch = false; try { Regex regex = new Regex(@"&lt;([be])pt[^&gt;]+&gt;.+?&lt;/\1pt&gt;"); while(regex.IsMatch(yourstring) ) { yourstring = regex.Replace(yourstring, ""); } } catch (ArgumentException ex) { // Syntax error in the regular expression } </code></pre> <p>ADDITIONAL NOTES:</p> <p>In the comments a user expressed worry that the '.' pattern matcher would be cpu intensive. While this is true in the case of a standalone greedy '.', the use of the non-greedy character '?' causes the regex engine to only look ahead until it finds the first match of the next character in the pattern versus a greedy '.' which requires the engine to look ahead all the way to the end of the string. I use <a href="http://www.regexbuddy.com/" rel="noreferrer">RegexBuddy</a> as a regex development tool, and it includes a debugger which lets you see the relative performance of different regex patterns. It also auto comments your regexes if desired, so I decided to include those comments here to explain the regex used above:</p> <pre><code> // &lt;([be])pt[^&gt;]+&gt;.+?&lt;/\1pt&gt; // // Match the character "&lt;" literally «&lt;» // Match the regular expression below and capture its match into backreference number 1 «([be])» // Match a single character present in the list "be" «[be]» // Match the characters "pt" literally «pt» // Match any character that is not a "&gt;" «[^&gt;]+» // Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» // Match the character "&gt;" literally «&gt;» // Match any single character that is not a line break character «.+?» // Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?» // Match the characters "&lt;/" literally «&lt;/» // Match the same text as most recently matched by backreference number 1 «\1» // Match the characters "pt&gt;" literally «pt&gt;» </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