Parsing Strings in Enclosed in Curly Braces

Rhodri James rhodri at wildebst.demon.co.uk
Sat May 16 00:18:14 CEST 2009


On Fri, 15 May 2009 17:12:24 +0100, <xamalek at yahoo.com> wrote:

> How do you parse a string enclosed in Curly Braces?
>
> For instance:
>
> x = "{ABC EFG IJK LMN OPQ}"
>
> I want to do x.split('{} ') and it does not work. Why does it not work
> and what are EXCEPTIONS to using the split method?

Other people have already done this bit :-)

> That I want to split based on '{', '}' and WHITESPACE.

This is not the same as what you first said, and I have a horrid feeling
you're trying to conflate two steps into one.  That way lies madness.

First, a question.  Is 'x' truly a good representation of your original
data?  Could it instead look more like:

x = "Ignore this {but not this} and completely forget about this"

Can you have braces legitmately lying around in the string, escaped
somehow:

x = r"{Parse including the \} escaped close brace}"

Can you have nested braces, and what are you supposed to do with them:

x = "{Some text to parse {as well as an aside} and so on}"

Parsing is not an entirely trivial subject, particularly when users
can futz about with the strings you're parsing.  That's one reason there
are so many lexers and parsers out there!

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list