Regexes have limitations with stream parsing but something like this will probably do the trick. You need the 1 while as in the last example the first pass removes the inner null tag pair and the second pass removes the enclosing pair now that the inner null pair has been replaced by a null string. By nothing between I have taken that to be literal. I have also assumed that the general syntax is [blah...]...[/blah].

The capture into $1 (\1) assumes there is no space between the [ and the blah.... If it is not well formed (ie humans, not a machine generated it) it is generally useful to put lots of \s* tokens into the RE so that it can deal with [ blah=foo]    [ /  blah]

local $/; $_ = <DATA>; 1 while s#\[(\w+)[^\]]*\]\[/\1\]##ig; print; __DATA__ [b]bold text[/b] [color=Red]Red text text[/color] [color=Red][b]Red bold text[/b][/color] I need to remove the ones below: [b][/b] [color=Red][/color] [color=Red][b][/b][/color]

cheers

tachyon


In reply to Re: Regex help by tachyon
in thread Regex help by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.