[英]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.