2013-01-10 48 views
0

比方说,我有这样的线串:字符串拆分与delimeters保留在字符串结果数组,如何? 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] "]]"

等等,所以基本上我的定位是{"'''","{{","}}","[[","]]"} 只是需要它的方式,我会编辑以后。如果它不会与正则表达式会更好。

+0

为什么你不希望使用正则表达式? – Servy

+0

@Servy regex.Split也删除了分隔符 –

+0

通常它会是http://stackoverflow.com/questions/2910536/regex-split-string-but-keep-separators的重复,但你明确地想要其他解决方案... –

回答

3

使用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); 
+0

你只需要删除空的entires;除此之外它匹配请求的输出。 – Servy

+0

+1几乎确切的结果...但OP因某种原因想要解决方案。 –

+0

好的谢谢,我会采取因为现在我可以理解的语法,我希望我未来的变化不会复杂化,谢谢。 –

0

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