Getting Unterminated [ ] set exception in Regex.Split() RRS feed

  • Question

  • Hi All, 

    I am getting "parsing "[]" - Unterminated [] set."  exception while splitting string with Regular expression for below string

    string studentinfo = "Student Info : \n[]Student Attendance \n[]Student Lunch Time\n[]Student Play time\n[]Student Library time, \nStudent Class room Info : \n[]Maths Class Time \n[]Physics Class Time \n[]GK Class Time \n\nFinal Report";
                List<string> pattern = new List<string>();
                string[] studentRecord = null;
                    if (studentinfo != null && pattern != null)
                        studentRecord = Regex.Split(studentinfo, @"" + pattern[0] + "", RegexOptions.IgnoreCase).Where(s => s.Contains(pattern[0]) && s != string.Empty).ToArray().Select(p => p.Replace(pattern[0], string.Empty)).ToArray();
                catch (Exception e)
    what is the mistake here? please help me



    Thursday, July 26, 2018 6:25 AM

All replies

  • I assume you are trying to match on the [] characters in your string. But [ and ] are special characters in regex (see here).

    So you need to escape them:


    Still not sure this will produce the results you are after though. You may have to explain what you're trying to do.

    Thursday, July 26, 2018 8:34 AM
  • I will be having the string with such kind of patterns like for eg : "[]", "[__]", "(**)" etc. i need to split that string according to that patterns . 

    And in above string "Student Info : \n" doesn't have pattern before. so i don't need that part as list entry. 

    Thursday, July 26, 2018 9:22 AM
  • Hi,

    Do you mean that you want to remove "\n" in your string?

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 27, 2018 9:15 AM