Regex separate curly brackets

ClaudeWalker used Ask the Experts™
I am trying to parse records delimited by curly brackets.  I'd like to use both brackets as delimiters in a regex split operation

{name:"Tom", job: "doctor", bday:1/1/91}, {name:"Jim", job: "nurse", bday:1/1/91}, {name:"Sara", job: "Admin", bday:1/1/91}

records[0] = name:"Tom", job: "doctor", bday:1/1/91
records[1] = name:"Jim", job: "nurse", bday:1/1/91
records[2] = name:"Sara", job: "Admin", bday:1/1/91

I'd like to use Regex to separate the records.  I can't use commas because they are used elsewhere.  I can't figure out the grammar to parse it properly

JOe K.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here is the regex that would do what you want:

Open in new window


I meant to explain that before posting.  Backslash to search for a literal {, then any number of characters that aren't curly brackets followed by a curly bracket.  This is better than using a .* since the * is a greedy quantifier.  You might also get:


Open in new window

but I believe the first option will be better.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

You will need to escape those backslashes in C#, so the full pattern string would be:

Open in new window


Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

I can't use commas because they are used elsewhere
Could you not use the sequence "}, {" to split on? Then a simple string split would suffice. You would, of course, need to trim the starting "{" and ending "}".

string testing = "{name:\"Tom\", job: \"doctor\", bday:1/1/91}, {name:\"Jim\", job: \"nurse\", bday:1/1/91}, {name:\"Sara\", job: \"Admin\", bday:1/1/91}";

string[] records = testing.TrimStart('{')
                          .Split(new string[] { "}, {" }, StringSplitOptions.RemoveEmptyEntries);

Open in new window

You don't have to escape the braces that occur inside of the bracket expression  : )
Senior Software Engineer

Please have a look at this as well:

string Unparsed = "{name:\"Tom\", job: \"doctor\", bday:1/1/91}, {name:\"Jim\", job: \"nurse\", bday:1/1/91}, {name:\"Sara\", job: \"Admin\", bday:1/1/91}";
List<string> ParsedList = new List<string>(Regex.Split(Unparsed.Replace("{", "").TrimEnd("}"), "}, "));

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial