skip to Main Content

That code makes filter on the files with the regular expression, and to shape the results. But how can I get a line number of each matches in the file?
I have no idea how to do it…
Please help me

class QueryWithRegEx  
enter code here


   
    IEnumerable<System.IO.FileInfo> fileList = GetFiles(startFolder);  

   
    System.Text.RegularExpressions.Regex searchTerm =  
        new System.Text.RegularExpressions.Regex(@"Visual (Basic|C#|C++|Studio)");  


    var queryMatchingFiles =  
        from file in fileList  
        where file.Extension == ".htm"  
        let fileText = System.IO.File.ReadAllText(file.FullName)  
        let matches = searchTerm.Matches(fileText)  
        where matches.Count > 0  
        select new  
        {  
            name = file.FullName,  
            matchedValues = from System.Text.RegularExpressions.Match match in matches  
                            select match.Value  
        };  


    Console.WriteLine("The term "{0}" was found in:", searchTerm.ToString());  

    foreach (var v in queryMatchingFiles)  
    {  
         
        string s = v.name.Substring(startFolder.Length - 1);  
        Console.WriteLine(s);  

      
        foreach (var v2 in v.matchedValues)  
        {  
            Console.WriteLine("  " + v2);  
        }  
    }  


    Console.WriteLine("Press any key to exit");  
    Console.ReadKey();  
}  

static IEnumerable<System.IO.FileInfo> GetFiles(string path)  
{  
    ...}

3

Answers


  1. You can Understand from this example:

    Read lines number 3 from string

    string line = File.ReadLines(FileName).Skip(14).Take(1).First();
    

    Read text from a certain line number from string

    string GetLine(string text, int lineNo) 
    { 
      string[] lines = text.Replace("r","").Split('n'); 
      return lines.Length >= lineNo ? lines[lineNo-1] : null; 
    }
    
    Login or Signup to reply.
  2. Give this a go:

    var queryMatchingFiles =
        from file in Directory.GetFiles(startFolder)
        let fi = new FileInfo(file)
        where fi.Extension == ".htm"
        from line in File.ReadLines(file).Select((text, index) => (text, index))
        let match = searchTerm.Match(line.text)
        where match.Success
        select new
        {
            name = file,
            line = line.text,
            number = line.index + 1
        };
    
    Login or Signup to reply.
  3. Retrieving the line number from a Regex match can be tricky, I’d use the following approach:

    private static void FindMatches(string startFolder)
    {
        var searchTerm = new Regex(@"Visual (Basic|C#|C++|Studio)");
    
        foreach (var file in Directory.GetFiles(startFolder, "*.htm"))
        {
            using (var reader = new StreamReader(file))
            {
                int lineNumber = 1;
                string lineText;
                while ((lineText = reader.ReadLine()) != null)
                {
                    foreach (var match in searchTerm.Matches(lineText).Cast<Match>())
                    {
                        Console.WriteLine($"Match found: <{match.Value}> in file <{file}>, line <{lineNumber}>");
                    }
    
                    lineNumber++;
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search