views:

398

answers:

4

I need to extract tokens that are marked with curly brackets from a given string.

I have tried using Expresso to construct something that will parse...

-------------------------------------------------------------
"{Token1}asdasasd{Token2}asd asdacscadase dfb db {Token3}"
-------------------------------------------------------------

and produce "Token1", "Token2", "Token3"

I tried using..

-------------------------------------------------------------
({.+})
-------------------------------------------------------------

...but that seemed to match the entire expression.

Any thoughts?

+2  A: 

Try:

\{([^}]*)\}

This will clamp the search inside of squiggly braces to stop on the closing brace.

sixlettervariables
To escape or not... that is the key question ! ;-)
Cerebrus
You shouldn't have to escape the } in the character class, if that is what you mean. Also this is slightly more readable (intent wise) than an ungreedy .*
sixlettervariables
+6  A: 

Try

\{(.*?)\}
The \{ will escape the "{" (which has meaning in a RegEx).
The \} likewise escapes the closing } backet.
The .*? will take minimal data, instead of just .* 
which is "greedy" and takes everything it can.
If you have assurance that your tokens will (or need to) 
be of a specific format, you can replace .* with an appropriate 
character class. For example, in the likely case you 
want only words, you can use (\w*) in place of the (.*?) 
This has the advantage that closing } characters are not 
part of the class being matched in the inner expression, 
so you don't need the ? modifier). 
Godeke
+2  A: 

Curly braces have special meaning in regular expressions, so you have to escape them. Use \{ and \} to match them.

Brian Rasmussen
+1  A: 

Another solution:

(?<=\{)([^\}]+)(?=\})

This uses a lookahead and a lookbehind so the brackets aren't consumed at all.

Evan Fosmark