如何将项目添加到树视图而不将其复制到具有相同名称的其他节点
How to add items to treeview without duplicating it to other node with the same name
我的编码有问题。我想使用正则表达式和 c# 将项目添加到 treeview。但问题是要添加的节点具有相同的名称。
The code ::
treeView1.Nodes.Clear();
string name;
TreeNode Root = new TreeNode();
RootNode.Text = "RootNode";
treeView1.Nodes.Add(RootNode);
MatchCollection ToGetPulinName = Regex.Matches(richtextbox1.Text,@"pulin (.*?)\{");
foreach (Match m in ToGetPulinName)
{
Group g = m.Groups[1];
name= g.Value;
TreeNode SecondRoot = new TreeNode();
SecondRoot.Text = "PulinRoot:"+pulinname;
RootNode.Nodes.Add(SecondRoot);
MatchCollection ToFoundIn = Regex.Matches(codingtxt.Text, @"pulin \w+{(.*?)}", RegexOptions.Singleline);
foreach (Match min in ToFoundIn)
{
Group gMin = min.Groups[1];
string gStr = gMin.Value;
string classname;
MatchCollection classnames = Regex.Matches(codingtxt.Text, @"class (.*?)\{", RegexOptions.IgnoreCase);
foreach (Match mis in classnames)
{
Group gmis = mis.Groups[1];
classname = gmis.Value;
TreeNode ClassRoot = new TreeNode();
ClassRoot.Text = "ClassRoot";
SecondRoot.Nodes.Add(ClassRoot);
}
}
}
The Result
请帮忙,谢谢。
问题在于您如何使用正则表达式解析数据,而不是辅助树视图,这在这里并不是真正的问题。
我们将使用以下模式搜索您的文本,然后 group/extract 将所有数据首先放入我所称的名称空间及其关联的 classes。
在模式中,通过使用 (?< > )
这是一个 命名匹配捕获 我们可以更轻松地提取数据。下面创建了一个动态的 linq 实体来保存我们的数据。我正在展示 Linqpad 的 Dump()
扩展的结果,它为我们提供了数据。我还为这个例子添加了另一个 class 到测试器:
var data = @"
pulin Tester { class Main { } class Omega{} }
pulin Tester2 { }
";
var pattern = @"
pulin\s+ # Look for our anchor `pulin` which identifies the current namespace,
# it will designate each namespace block
(?<Namespace>[^\s{]+) # Put the namespace name into the named match capture `Namespace`.
( # Now look multiple possible classes
.+? # Ignore all characters until we find `class` text
(class\s # Found a class, let us consume it until we find the textual name.
(?<Class>[^\{]+) # Insert the class name into the named match capture `Class`.
)
)* # * means 0-N classes can be found.
";
// We use ignore pattern whitespace to comment the pattern.
// It does not affect the regex parsing of the data.
Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace)
.OfType<Match>()
.Select (mt => new
{
Namespace = mt.Groups["Namespace"].Value,
Classes = mt.Groups["Class"].Captures
.OfType<Capture>()
.Select (cp => cp.Value)
})
.Dump();
最终您将遍历以填充树的 Dump() 结果。:
我留给你用数据正确填充树。
请注意,我会失职提醒您,使用正则表达式进行词法解析有其固有的缺陷,最终最好使用适合该工作的真正解析工具。
我的编码有问题。我想使用正则表达式和 c# 将项目添加到 treeview。但问题是要添加的节点具有相同的名称。
The code ::
treeView1.Nodes.Clear();
string name;
TreeNode Root = new TreeNode();
RootNode.Text = "RootNode";
treeView1.Nodes.Add(RootNode);
MatchCollection ToGetPulinName = Regex.Matches(richtextbox1.Text,@"pulin (.*?)\{");
foreach (Match m in ToGetPulinName)
{
Group g = m.Groups[1];
name= g.Value;
TreeNode SecondRoot = new TreeNode();
SecondRoot.Text = "PulinRoot:"+pulinname;
RootNode.Nodes.Add(SecondRoot);
MatchCollection ToFoundIn = Regex.Matches(codingtxt.Text, @"pulin \w+{(.*?)}", RegexOptions.Singleline);
foreach (Match min in ToFoundIn)
{
Group gMin = min.Groups[1];
string gStr = gMin.Value;
string classname;
MatchCollection classnames = Regex.Matches(codingtxt.Text, @"class (.*?)\{", RegexOptions.IgnoreCase);
foreach (Match mis in classnames)
{
Group gmis = mis.Groups[1];
classname = gmis.Value;
TreeNode ClassRoot = new TreeNode();
ClassRoot.Text = "ClassRoot";
SecondRoot.Nodes.Add(ClassRoot);
}
}
}
The Result
请帮忙,谢谢。
问题在于您如何使用正则表达式解析数据,而不是辅助树视图,这在这里并不是真正的问题。
我们将使用以下模式搜索您的文本,然后 group/extract 将所有数据首先放入我所称的名称空间及其关联的 classes。
在模式中,通过使用 (?< > )
这是一个 命名匹配捕获 我们可以更轻松地提取数据。下面创建了一个动态的 linq 实体来保存我们的数据。我正在展示 Linqpad 的 Dump()
扩展的结果,它为我们提供了数据。我还为这个例子添加了另一个 class 到测试器:
var data = @"
pulin Tester { class Main { } class Omega{} }
pulin Tester2 { }
";
var pattern = @"
pulin\s+ # Look for our anchor `pulin` which identifies the current namespace,
# it will designate each namespace block
(?<Namespace>[^\s{]+) # Put the namespace name into the named match capture `Namespace`.
( # Now look multiple possible classes
.+? # Ignore all characters until we find `class` text
(class\s # Found a class, let us consume it until we find the textual name.
(?<Class>[^\{]+) # Insert the class name into the named match capture `Class`.
)
)* # * means 0-N classes can be found.
";
// We use ignore pattern whitespace to comment the pattern.
// It does not affect the regex parsing of the data.
Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace)
.OfType<Match>()
.Select (mt => new
{
Namespace = mt.Groups["Namespace"].Value,
Classes = mt.Groups["Class"].Captures
.OfType<Capture>()
.Select (cp => cp.Value)
})
.Dump();
最终您将遍历以填充树的 Dump() 结果。:
我留给你用数据正确填充树。
请注意,我会失职提醒您,使用正则表达式进行词法解析有其固有的缺陷,最终最好使用适合该工作的真正解析工具。