Avatar of ClaudeWalker
ClaudeWalker
 asked on

Regex separate curly brackets

I am trying to parse records delimited by curly brackets.  I'd like to use both brackets as delimiters in a regex split operation

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}

Parsed:
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

Thanks,
JOe K.
 
.NET ProgrammingRegular Expressions

Avatar of undefined
Last Comment
disrupt

8/22/2022 - Mon
disrupt

SterlingMcClung

Here is the regex that would do what you want:
 
\{[^\}\{]*\}

Open in new window

SterlingMcClung

Sorry,

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SterlingMcClung

You will need to escape those backslashes in C#, so the full pattern string would be:
 
"\\{[^\\{\\}]\\}"

Open in new window

or
 
"\\{.*?\\}"

Open in new window

kaufmed

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('{')
                          .TrimEnd('}')
                          .Split(new string[] { "}, {" }, StringSplitOptions.RemoveEmptyEntries);

Open in new window



@SterlingMcClung
You don't have to escape the braces that occur inside of the bracket expression  : )
ASKER CERTIFIED SOLUTION
Shahan Ayyub

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.