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));
}
}
}
}
*?
将尝试使用尽可能少的字符@“\[\[(?:(.*?)\\\\\\\\\\\\\\..*(.*?//(.*?)(.*?)\]\]\]”
(将尽可能长的备选方案放在第一位很重要,否则*?
可能会吃掉整个字符串)File.ReadLines
以及一个变量,您将在每次迭代中递增以计算行数。这样您就不必将整个文件保存在内存中只有一个问题:我在匹配项中获得[[[和]]]。。如何删除它们?不要使用整个匹配项。而是使用。更具体地说,使用
match.Groups[1]
和match.Groups[2]
(对于最后一个,请检查其Success
属性)。