C# Regex Error Unterminated [] set

c#

Solution

From the Regex website

Because we want to do more than simply search for literal pieces of text, we need to reserve certain characters for special use. In the regex flavors discussed in this tutorial, there are 11 characters with special meanings: the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ). These special characters are often called "metacharacters".

Escape the brackets with a \

(--\[\[.*?]])|(--\[\[.*)

Problem

I'm receiving this error (I'm using C#): parsing "(--[[.?]])|(--[[.)" - Unterminated [] set. When trying to add Lua's multiple comment code. Here is what it should be: ``` --[[ Hello ]] ``` However, when I take out the ']]' at the end of this Regex: ``` (--[[.*?]])|(--[[.*) ``` it will give me this error. However, if I add the ']]' in the above, example: ``` (--[[.*?]])|(--[[.*]]) ``` it works perfectly fine. Here is the full code: ``` Regex CustomCommentRegex1, CustomCommentRegex2, CustomCommentRegex3; CustomCommentRegex1 = new Regex(@"--.*$", RegexOptions.Multiline | RegexCompiledOption); CustomCommentRegex2 = new Regex(@"(--[[.*?]])|(--[[.*)", RegexOptions.Singleline | RegexCompiledOption); CustomCommentRegex3 = new Regex(@"(--[[.*?]])|(.*]])", RegexOptions.Singleline | RegexOptions.RightToLeft | RegexCompiledOption); ``` 'CustomCommentRegex2' is where I get this 'Unterminated [] set' error. I have an issue if I add the ']]' at the end of 'CustomCommentRegex2'. It will highlight the text above '--[[ ]]' the comments and inside of it; anyway, the issue is this 'Unterminated [] set' error.

Original source