简体   繁体   English

如何找到大括号内未包含的文本

[英]How to find the text inside that are not contained inside braces

I have a text like test{0:##}test2{Order:C}test3 . 我有一个像test{0:##}test2{Order:C}test3的文本。 I need to use regex and find the text test , test2 , test3 which are not inside the {...} . 我需要使用正则表达式并找到不在{...}内的文本testtest2test3

For text inside the curly braces {0:##} i used 对于我使用的花括号{0:##}内的文字

@"{0:(.*?)}"

But trying for text not inside the Curly braces, facing some difficulties. 但是尝试不在Curly括号内的文字,面临一些困难。

If there are no nested braces you can use something like the following: 如果没有嵌套大括号,您可以使用以下内容:

(?<=^|})[^{]+(?={|$)

This uses lookbehind and lookahead to find snippets embedded in }...{ or delimited by the start or end of the string on one end. 这使用lookbehind和lookahead来查找嵌入在}...{或由一端的字符串的开头或结尾分隔的片段。

Quick PowerShell test: 快速PowerShell测试:

PS> [regex]::Matches('test{0:##}test2{Order:C}test3', '(?<=^|})[^{}]+(?={|$)') | select Value

Value
-----
test
test2
test3

而不是匹配大括号之外的所有内容(如Joey所建议的),通过仅匹配大括号的表达式将字符串拆分为数组可能更容易。

result = Regex.Split(teststring, "\{[^}]*\}");

如果文本始终采用相同的格式,为什么不使用Split功能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM