local $/; $_ = ; 1 while s#\[\s*(\w+)\s*(=[^\]]+)*\s*\]\s*\[\s*/\s*\1\s*\]##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] [color=Red][/col] Now this is still here ysth (if that is what is wanted) [ b ] [ / b ] This will go now [ foo = bar ] [ / foo ] ... and this too