预先编写代码以可视化我面临的问题:
- 这是需要拆分的文本。
:20:0444453880181732 :21:0444453880131350 :22:CANCEL/ABCDEF0131835055 :23:BUY/CALL/E/EUR :82A:ABCDEFZZ80A :87A:4444655604 :30:061123 :31G:070416/1000/USNY :31E:070418 :26F:PRINCIPAL :32B:EUR1000000,00 :36:1,31000000 :33B:USD1310000,00 :37K:PCT1,60000000 :34P:061127USD16000,00 :57A:ABCDEFZZ80A
这是我的正则表达式
Regex r = new Regex(@"\:\d{2}\w*\:", RegexOptions.Multiline);
MatchCollection matches = r.Matches(Content);
string[] items = r.Split(Content);
// ----- Fix for first entry being empty string.
int index = items[0] == string.Empty ? 1 : 0;
foreach (Match match in matches)
{
MessageField field = new MessageField();
field.FieldIdExtended = match.Value;
field.Content = items[index];
Fields.Add(field);
index++;
}
正如您从评论中看到的那样,字符串拆分会出现问题。它作为第一项返回一个空字符串。有什么优雅的方法可以解决这个问题吗?
谢谢,迪米