正如我在标题中所说的那样,我认为这个想法是通过像\d+?=.*?\d=
这样的东西来分割它,但不太确定......任何想法如何最好地解析这个字符串:
1=Some dummy sentence
2=Some other sentence 3=Third sentence which can be in the same line
4=Forth sentence
some text which shouldn't be captured and spplitted
我希望从中得到的是一个字典,它将具有该键的数字,并且该字符串在值中,例如:
1, "Some dummy sentence"
2, "Some other sentence"
3, "Third sentence which can be in the same line"
4, "Forth sentence"
答案 0 :(得分:1)
这个怎么样:https://regex101.com/r/6ED8Om/2
\n?(\d+)=(.*?)(?= *\d|\n)
\n?(\d+)=
匹配可选的换行符,后跟数字和等号(.*?)
符合以下文字(?= *\d|\n)
匹配任意数量的空格,后跟数字或新行字符。空间阻止#2在其末端#3 编辑:使用此正则表达式的其他答案代码将您的值保存到词典。组1匹配数字,组2匹配文本。
答案 1 :(得分:1)
将文本解析为字典的方法:
public static Dictionary<int, string> GetValuesToDictionary(string text)
{
var pattern = @"(\d+)=(.*?)((?=\d=)|\n)";
//If spaces between digit and equal sign are possible then (\d+)\s*=\s*(.*?)((?=\d\s?=)|\n)
var regex = new Regex(pattern);
var pairs = new Dictionary<int, string>();
var matches = regex.Matches(text);
foreach (Match match in matches)
{
var key = int.Parse(match.Groups[1].Value);
var value = match.Groups[2].Value;
if (!pairs.ContainsKey(key))
{
pairs.Add(key, value);
}
//pairs.Add(key, value);
}
return pairs;
}
在这种情况下,我检查lkey是否已经存在,如果是,我不添加它,但如果你需要这个检查,你可以自己看看。 包括没有等值的数字组。