Reputation: 15701
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?
Upvotes: 2
Views: 6021
Reputation: 101671
Another solution:
(?<=\{)([^\}]+)(?=\})
This uses a lookahead and a lookbehind so the brackets aren't consumed at all.
Upvotes: 2
Reputation: 16281
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).
Upvotes: 6
Reputation: 116401
Curly braces have special meaning in regular expressions, so you have to escape them. Use \{
and \}
to match them.
Upvotes: 1
Reputation: 64068
Try:
\{([^}]*)\}
This will clamp the search inside of squiggly braces to stop on the closing brace.
Upvotes: 2