AaBa AaBa -4 years ago 266
C# Question

C# Regular Expression throws Unterminated [] set




Hello <!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--> world!

C# code:

Regex clearMarkup = new Regex(@"\<!--\[\^]\+-->");
clearMarkup.Replace(str, ""); // str is the content as shown above.

Expected output:

Hello world!

The Regular expression created in http://www.regextester.com/ is working properly, however, in C# it throws error as:

Unterminated [] set

I got the same error in http://regexstorm.net/tester which is .Net RegEx tester.

I tried with:
and the issue got resolved, however, it does not match any of the contents.

Please advise, thanks.

Answer Source

Your problem is twofold. You need to use a RegexOption and assign the string back to str.

The RegexOptions.Singleline makes regex treat everything as if it is on one line and ignores the \n.

You also need to assign the returned string from clearMarkup.Replace to your original variable:

str = clearMarkup.Replace(str, "");

Here is a link to a working example: Example

    string str = @"Hello <!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
                        o\:* {behavior:url(#default#VML);}
                        w\:* {behavior:url(#default#VML);}
                        .shape {behavior:url(#default#VML);}
                        </style><![endif]--> world!";

Regex clearMarkup = new Regex(@"(<!--\[.*\]-->)", RegexOptions.Singleline);
str = clearMarkup.Replace(str, ""); // str is the content as shown above.       

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download