Как извлечь все токены {} из строки using.Net-это регулярное выражение?
Мне нужно извлечь маркеры, помеченные фигурными скобками из данной строки.
Я пробовал использовать Expresso, чтобы построить что-то, что будет разбираться...
-------------------------------------------------------------
"{Token1}asdasasd{Token2}asd asdacscadase dfb db {Token3}"
-------------------------------------------------------------
И произвести "Token1", "Token2", "Token3"
Я попробовал использовать..
-------------------------------------------------------------
({.+})
-------------------------------------------------------------
...но это, казалось, соответствовало всему выражению лица.
Есть какие-нибудь мысли?
4 ответа:
Попробуйте
\{(.*?)\}
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).
Попробуйте:
\{([^}]*)\}
Это зажмет поиск внутри волнистых скобок, чтобы остановиться на закрывающей скобке.