如何获取包含仅从字符串开始[和结束] [英] how to get contain start with [ and end with ] only from string
本文介绍了如何获取包含仅从字符串开始[和结束]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从[和结束]开始包含仅来自字符串
[ANC 2 DATE] = [yes]和[AllIFADone] = [YES]和
i想要在阵列中出来
喜欢
[ANC 2 DATE]
[是]
[AllIFADone]
其他应该得到忽略
how to get contain start with [ and end with ] only from string
[ANC 2 DATE] = [yes] AND [AllIFADone] = [YES] AND
i want out out in array
like
[ANC 2 DATE]
[yes]
[AllIFADone]
other should get ignore
推荐答案
会员-515487的解决方案可行,但平衡组的使用更好:
Member-515487's solution will work, but the use of Balancing Groups is a better idea:
(?<sb>\[).*?(?<-sb>\])
这可以不用使用RegEx:
This can be done without using RegEx:
char[] splitChars = new[] { '[' };
string anc = @"[ANC 2 DATE] = [yes] AND [AllIFADone] = [YES] AND";
string[] ancSplit = anc.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)
.Select(strx => ('[' + strx.Substring(0, strx.IndexOf(']') + 1))).ToArray();
但是,我没有声称这样做比使用RegEx要好。
样本结果:
However, I am not claiming that doing it this way is better than using a RegEx.
Sample result:
? ancSplit
{string[4]}
[0]: "[ANC 2 DATE]"
[1]: "[yes]"
[2]: "[AllIFADone]"
[3]: "[YES]"
string str = "[ANC 2 DATE] = [yes] AND [AllIFADone] = [YES] AND ";
var pattern = @"(\[(?:.*?)\])";
MatchCollection mcol = Regex.Matches(str, pattern);
foreach (Match m in mcol)
{
Console.WriteLine(m);
}
这篇关于如何获取包含仅从字符串开始[和结束]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文