【发布时间】:2015-06-04 00:55:24
【问题描述】:
我有如下字符串:
[Testing.User]|Info:([Testing.Info]|Name:([System.String]|Matt)|Age:([System.Int32]|21))|Description:([System.String]|This is some description)
你可以把它看成这棵树:
- [Testing.User]
- Info
- [Testing.Info]
- Name
- [System.String]
- Matt
- Age
- [System.Int32]
- 21
- Description
- [System.String]
- This is some description
如你所见,它是一个类Testing.User的字符串序列化/表示
我希望能够进行拆分并在结果数组中获取以下元素:
[0] = [Testing.User]
[1] = Info:([Testing.Info]|Name:([System.String]|Matt)|Age:([System.Int32]|21))
[2] = Description:([System.String]|This is some description)
我不能按| 拆分,因为这会导致:
[0] = [Testing.User]
[1] = Info:([Testing.Info]
[2] = Name:([System.String]
[3] = Matt)
[4] = Age:([System.Int32]
[5] = 21))
[6] = Description:([System.String]
[7] = This is some description)
我怎样才能得到我的预期结果?
我不太擅长正则表达式,但我知道这是一种非常可能的解决方案。
【问题讨论】:
-
你必须把它序列化成那种格式吗?或者你可以放入像 JSON 或 XML 这样更容易反序列化的东西吗?
-
@Steve 不幸的是,是的。它必须是这种格式:(
-
这很困难,因为它不是任何常规格式。
-
这个任务是写反序列化器来序列化
Testing.User吗?如果它不是,是否可以寻找Testing.User的反序列化器并进行自己的重新设计?上面已经建议序列化为另一种格式,我强烈建议如果您/您的团队可以拨打电话来切换序列化库,特别是如果它是定制一个。在这个时代,序列化不需要重新开发。 :)
标签: c# regex string parsing split