C# 使用RegularExpression更正关联
我有两门课,主题和设置。 主题具有主题名和设置列表作为属性。 设置有一个键、一个值和另一个设置列表作为可选项 现在,我有一个主题列表和一个从数据库检索到的设置列表。 我想把好的环境与好的(主题/环境)联系起来 例如:C# 使用RegularExpression更正关联,c#,regex,C#,Regex,我有两门课,主题和设置。 主题具有主题名和设置列表作为属性。 设置有一个键、一个值和另一个设置列表作为可选项 现在,我有一个主题列表和一个从数据库检索到的设置列表。 我想把好的环境与好的(主题/环境)联系起来 例如: Topic#1.TopicName = Grandpa Setting#1.Key = Grandpa/Papa Setting#2.Key = Grandpa/Mama Setting#3.Key = Grandpa/Papa/Baby 根据我的格式,主题1有一个设置列表(设置
Topic#1.TopicName = Grandpa
Setting#1.Key = Grandpa/Papa
Setting#2.Key = Grandpa/Mama
Setting#3.Key = Grandpa/Papa/Baby
根据我的格式,主题1有一个设置列表(设置1和设置2)
设置#2包含设置列表(设置#3)
我现在的代码是
var topicList = new List<Topic>();
foreach (var topics in topicRetrieved)
{
var topic = new Topic()
{
TopicName = topics.Key,
Override = topics.Override,
Source = topics.Source,
};
foreach (var topicSettings in topicSettingRetrieved)
{
string parent = topic.TopicName + "/";
if(System.Text.RegularExpressions.Regex.IsMatch(topicSettings.Key,parent))
{
var setting = new Setting
{
Key = topicSettings.Key,
Value = topicSettings.Value
};
topic.SettingCollection.Add(setting);
}
}
topicList.Add(topic);
}
return topicList.ToArray();
}
var topicList=newlist();
foreach(topicRetrieved中的var主题)
{
var topic=新主题()
{
TopicName=topics.Key,
覆盖=主题。覆盖,
Source=topics.Source,
};
foreach(检索到topicSettings中的var topicSettings)
{
字符串parent=topic.TopicName+“/”;
if(System.Text.RegularExpressions.Regex.IsMatch(topicSettings.Key,父项))
{
var设置=新设置
{
键=主题设置。键,
值=topicSettings.Value
};
topic.SettingCollection.Add(设置);
}
}
主题列表。添加(主题);
}
返回topicList.ToArray();
}
我现在的问题是,主题#1将始终具有设置列表
(#1、#2和#3),因为它包含字符串“爷爷”。
我只希望主题#1包含设置#1和设置#2
我希望能够将设置#3添加到正确的位置(设置#2)
设置#3=爷爷/爸爸/宝宝------->设置#2=爷爷/爸爸
然后
我的设置2将转到主题1
有什么想法吗?试试这个
字符串parent=“^”+topic.TopicName+”/[^/]*$”代码>您是否尝试在所有设置中匹配“爷爷”,但前提是设置字符串中只有一个/
(而不是两个)?