[英]Use regex to split a string by {curly} substrings but include the { }
I have this string: 我有这个字符串:
var s = "Cat{biscuits}Flaps";
Output should be: 输出应为:
Cat
猫
{biscuits}{饼干}
Flaps襟翼
I can iterate over this: 我可以对此进行迭代:
Regex.Split(input, @"{(?<p>[^}]*)}");
However it removes the { } from biscuits 但是它将{}从饼干中删除
Can you suggest a regex that includes the { } to give the output above? 您能否建议一个包含{}的正则表达式来给出上面的输出?
Use a capturing group around the whole pattern: 对整个模式使用捕获组:
var chunks = Regex.Split(input, @"({[^}]*})");
^ ^
var s = "Cat{biscuits}Flaps";
var chunks = Regex.Split(s, @"({[^}]*})");
Console.WriteLine(string.Join("\n", chunks));
See Regex.Split
reference : 请参阅
Regex.Split
参考 :
If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array.
如果在Regex.Split表达式中使用了捕获括号,则任何捕获的文本都将包含在结果字符串数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.