用分隔符分割的字符串如何留在字符串结果数组中?c#

本文关键字:字符串 数组 结果 何留 分隔符 分割 | 更新日期: 2023-09-27 17:52:15

假设我有这样一行字符串:

"温斯顿·伦纳德·斯宾塞-丘吉尔爵士",{{后名格|后名格=[[嘉德骑士|KG]]

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

<>之前[0]""[1]"温斯顿·伦纳德·斯宾塞-丘吉尔爵士"[2]""[3] ", "[4]"{{"[5]"Post-nominals | post-noms = "[["[6][7]《嘉德骑士》|KG[8]]]"之前

等等,所以我的定界是{"'''","{{","}}","[[","]]"}只是需要它的方式,我稍后会编辑它。如果不使用正则表达式,那就更好了。

用分隔符分割的字符串如何留在字符串结果数组中?c#

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

使用(?<=^?'''|'{'{|'['[|']']|'}'})(.+?)(?=$?'''|'{'{|'['[|']']|'}'})