【发布时间】:2017-11-14 14:14:50
【问题描述】:
我需要使用 c# 正则表达式拆分输入字符串。 需要知道如何在输出中包含分隔符内容,如下所示。
输入:
string content="heading1: contents with respect to heading1 heading2: heading2 contents heading3: heading 3 related contents sample strings";
string[] delimters = new string[] {"heading1:","heading2:","heading3:"};
预期输出:
outputArray[0] = heading1: contents with respect to heading1
outputArray[1] = heading2: heading2 contents
outputArray[2] = heading3: heading 3 related contents sample strings
我尝试了什么:
var result = content.Split(delimters,StringSplitOptions.RemoveEmptyEntries);
我得到的输出:
result [0]: " contents with respect to heading1 "
result [1]: " heading2 contents "
result [2]: " heading 3 related contents sample strings"
我在 string.split 或 Regex 中找不到要拆分为预期结果的 API。
【问题讨论】:
-
请发布失败的正则表达式,否则问题离题,必须关闭。
-
开始搜索以下
IndexOf(), string.Split(), and string.Join()函数.. 祝你好运 -
这个
\S+:.*?(?=\S+:|$)可能适用于您的情况。 -
@MethodMan:谢谢。我正在阅读有关正则表达式的更多信息以找到最佳解决方案。一旦我发现我会发布答案。