繁体   English   中英

带分隔符的字符串拆分保留在字符串结果数组中,怎么办? C#

[英]string split with delimeters remain in strings result array, how? c#

假设我有以下行字符串:

'''Sir Winston Leonard Spencer-Churchill''', {{Post-nominals|post-noms=[[Knight of the Garter|KG]]

我已经在代码中使用了没有空结果的字符串拆分,但是我现在想要该字符串的结果为:

[0] "'''"
[1] "Sir Winston Leonard Spencer-Churchill"
[2] "'''"
[3] ", "
[4] "{{"
[5] "Post-nominals|post-noms="
[6] "[["
[7] "Knight of the Garter|KG"
[8] "]]"

依此类推,所以基本上我的分度是{"'''","{{","}}","[[","]]"}只需要这样,我稍后再编辑。 如果不使用正则表达式会更好。

使用正则Regex

string input = "'''Sir Winston Leonard Spencer-Churchill''', {{Post-nominals|post-noms=[[Knight of the Garter|KG]]";
string pattern = @"('''|\{\{|\}\}|\[\[|\]\])";
string[] result = Regex.Split(input, pattern);

使用此正则表达式(?<=^?'''|\\{\\{|\\[\\[|\\]\\]|\\}\\})(.+?)(?=$?'''|\\{\\{|\\[\\[|\\]\\]|\\}\\})

暂无
暂无

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

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