Chad Layton
Chad Layton

Reputation: 127

Parsing a string with nested brackets

I'm writing a application to parse some commands. Commands are given in the form:

A { B }

I just want A and B. A is optional but that's easy enough to handle. The problem I'm having is that both A and B can contain almost any character including whitespace and '{' and '}'. The brackets need not be balanced, either. Is this possible to parse with a regex? If not, what is the simplest thing that you think could be done?

For example, given:

"parsme { foo { "hello" } { "goodbye" } {{{ } { bar { "up" } { "down" } }"

Then:

A = "parseme { foo { "hello" } { "goodbye" } {{{ }" and B = "bar { "up" } { "down" }"

Upvotes: 0

Views: 1656

Answers (1)

Francis Upton IV
Francis Upton IV

Reputation: 19443

You can't use a regular expression to parse anything that requires arbitrary nesting like parenthesis (this is a well established limitation of regular expressions, a little googling here will help you).

You will need to use a context-free grammar for this using a tool like Antlr.

Upvotes: 5

Related Questions