gpt4 book ai didi

.net - 如何使用 .Net 的 RegEx 从字符串中提取所有 {} 标记?

转载 作者:行者123 更新时间:2023-12-05 01:01:42 24 4
gpt4 key购买 nike

我需要从给定的字符串中提取用大括号标记的标记。

我已经尝试使用 Expresso 来构造一些可以解析的东西......

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

并产生“Token1”、“Token2”、“Token3”

我试过使用..

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

...但这似乎与整个表达式匹配。

有什么想法吗?

最佳答案

尝试

\{(.*?)\}
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). 

关于.net - 如何使用 .Net 的 RegEx 从字符串中提取所有 {} 标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/492060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com