Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将项目添加到treeview,而不将其复制到具有相同名称的其他节点_C#_Regex_Treeview_Treenode - Fatal编程技术网

C# 如何将项目添加到treeview,而不将其复制到具有相同名称的其他节点

C# 如何将项目添加到treeview,而不将其复制到具有相同名称的其他节点,c#,regex,treeview,treenode,C#,Regex,Treeview,Treenode,我的编码有问题。我想使用regex和c#向treeview添加项目。但问题是要添加的节点具有相同的名称 代码:: 结果 请帮助,谢谢。问题在于如何使用regexs解析数据,而不是辅助树视图,这在这里不是真正的问题 使用下面的模式,我们将搜索文本并首先将所有数据分组/提取到我所称的名称空间及其关联类中 在模式中,通过使用(?)这是一种命名的匹配捕获,我们可以更轻松地提取数据。下面创建一个动态linq entites来保存我们的数据。我正在显示Linqpad的Dump()扩展的结果,该扩展为我们提

我的编码有问题。我想使用regex和c#向treeview添加项目。但问题是要添加的节点具有相同的名称

代码::

结果


请帮助,谢谢。

问题在于如何使用regexs解析数据,而不是辅助树视图,这在这里不是真正的问题

使用下面的模式,我们将搜索文本并首先将所有数据分组/提取到我所称的名称空间及其关联类中

在模式中,通过使用
(?<>)
这是一种命名的匹配捕获,我们可以更轻松地提取数据。下面创建一个动态linq entites来保存我们的数据。我正在显示Linqpad的
Dump()
扩展的结果,该扩展为我们提供了数据。此外,我还为这个示例向Tester添加了另一个类:

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();
var data=@”
pulin测试器{class Main{}class Omega{}
pulin测试器2{}
";
变量模式=@“
pulin\s+#查找标识当前名称空间的锚点'pulin',
#它将指定每个名称空间块
(?[^\s{]+)#将名称空间名称放入命名的匹配捕获`namespace`。
(#现在查看多个可能的类
.+?#忽略所有字符,直到找到'class'文本
(class\s#找到一个类,让我们使用它直到找到文本名称。
(?[^\{]+)#将类名插入命名的匹配捕获`class`。
)
)*#*表示可以找到0-N个类。
";
//我们使用忽略模式空白来注释模式。
//它不影响数据的正则表达式解析。
Regex.Matches(数据、模式、RegexOptions.IgnorePatternWhitespace)
第()类
.选择(mt=>new
{
Namespace=mt.Groups[“Namespace”].Value,
Classes=mt.Groups[“Class”]。捕获
第()类
.Select(cp=>cp.Value)
})
.Dump();
Dump()的结果,最终您将遍历该结果以填充树:

我让您用数据正确地填充树



请注意,我无意提醒您,使用正则表达式进行词法分析有其固有的缺陷,最终最好使用适合这项工作的真正的分析工具。

Hmm您已经理解了我。我想我需要了解更多。您能给我提供一个链接吗?如果您知道一个好的分析工具,并提供一些有用的经验,那么.Dump();*(System.Collection.Generic.IEnum..找不到.dumb)中的ks.Error@AhmedAlaa
Dump
不会在实际程序中使用。它仅在上可用以显示对象的当前状态。请删除Dump并处理随数据返回的动态实体。@AhmedAlaa请参阅以开始。
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();