Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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# 在文件行中使用正则表达式查找匹配项_C# - Fatal编程技术网

C# 在文件行中使用正则表达式查找匹配项

C# 在文件行中使用正则表达式查找匹配项,c#,C#,我正在从目录中读取文件列表并查找模式: A. [[[Something]]] > Get the string "Something" B. [[[Something///Comment]]] > Get the strings "Something" and "Comment" C. [[[Enter between %0 and %1 characters|||Val 1|||Val 2]]] >> Get the string before the first

我正在从目录中读取文件列表并查找模式:

A. [[[Something]]] > Get the string "Something"

B. [[[Something///Comment]]] > Get the strings "Something" and "Comment"

C. [[[Enter between %0 and %1 characters|||Val 1|||Val 2]]] >> Get the string before the first ||| which is "Enter between %0 and %1 characters"
因此,我尝试了以下方法:

IList<String> files = Directory.GetFiles(path, "*.cshtml", SearchOption.AllDirectories).ToList();

IDictionary<String, Tuple<Int32, String>> items = new Dictionary<String, Tuple<Int32, String>>();

Regex regex = new Regex(@"\[\[\[.*\]\]\]");

foreach (String file in files) {

  foreach (String line in File.ReadAllLines(file)) {

    MatchCollection matches = regex.Matches(line);

    foreach (Match match in matches) {

      if (match != null) {
        items.Add(match.Value, new Tuple<Int32, String>(number, file));
      }

    }

  }

}
IList files=Directory.GetFiles(路径“*.cshtml”,SearchOption.AllDirectories).ToList();
IDictionary items=新字典();
正则表达式正则表达式=新正则表达式(@“\[\[\[\[.\]\]\]”);
foreach(文件中的字符串文件){
foreach(文件中的字符串行。ReadAllLines(文件)){
MatchCollection matches=regex.matches(行);
foreach(匹配中的匹配){
如果(匹配!=null){
Add(match.Value,新元组(数字,文件));
}
}
}
}
注意:我使用ReadAllLines,因为我需要获得找到的每个匹配项的行号

我可以在以下方面得到一些帮助:

IList<String> files = Directory.GetFiles(path, "*.cshtml", SearchOption.AllDirectories).ToList();

IDictionary<String, Tuple<Int32, String>> items = new Dictionary<String, Tuple<Int32, String>>();

Regex regex = new Regex(@"\[\[\[.*\]\]\]");

foreach (String file in files) {

  foreach (String line in File.ReadAllLines(file)) {

    MatchCollection matches = regex.Matches(line);

    foreach (Match match in matches) {

      if (match != null) {
        items.Add(match.Value, new Tuple<Int32, String>(number, file));
      }

    }

  }

}
  • 在使用Regex@“[[.]]]”时,我发现一种情况是is不起作用:

    ViewInfo.Title(“[[[Title]]”)。Description(“[[[Description]]]”)

    我得到了标题]]]”。描述(“[[Description]]]]

  • 我无法应用规则(B)和(C)

  • 是否有可能提高性能或我的代码正常

  • 您需要一个ungreedy表达式:
    *?
    将尝试使用尽可能少的字符

  • 尝试以下方法:
    @“\[\[(?:(.*?)\\\\\\\\\\\\\\..*(.*?//(.*?)(.*?)\]\]\]”
    (将尽可能长的备选方案放在第一位很重要,否则
    *?
    可能会吃掉整个字符串)

  • 使用
    File.ReadLines
    以及一个变量,您将在每次迭代中递增以计算行数。这样您就不必将整个文件保存在内存中


  • 只有一个问题:我在匹配项中获得[[[和]]]。。如何删除它们?不要使用整个匹配项。而是使用。更具体地说,使用
    match.Groups[1]
    match.Groups[2]
    (对于最后一个,请检查其
    Success
    属性)。