1

I have a text like test{0:##}test2{Order:C}test3. I need to use regex and find the text test , test2 , test3 which are not inside the {...}.

For text inside the curly braces {0:##} i used

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

But trying for text not inside the Curly braces, facing some difficulties.

4

3 に答える 3

2

ネストされたブレースがない場合は、次のようなものを使用できます。

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

これは、後読みと先読みを使用して}...{、一方の端の文字列の先頭または末尾に埋め込まれた、または区切られたスニペットを見つけます。

クイック PowerShell テスト:

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

Value
-----
test
test2
test3
于 2012-07-23T12:16:17.447 に答える
1

(Joey が提案したように) 中かっこの外側のすべてを一致させる代わりに、中かっこのみに一致する式によって、文字列を配列に分割する方が簡単かもしれません。

result = Regex.Split(teststring, "\{[^}]*\}");
于 2012-07-23T12:36:05.637 に答える
0

Splitテキストが常に同じ形式である場合は、この関数を使用してみませんか?

于 2012-07-23T12:24:43.807 に答える