Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active November 24, 2023 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/077a13822324a5341ed40df6985f1c98 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/077a13822324a5341ed40df6985f1c98 to your computer and use it in GitHub Desktop.
Search in Files across Folders by Regex using C#
// Search by RegEx in multiple files across multiple folders using C#
// Creating an index folder and add document's folder to it
Index index = new Index("indexing-folder-path");
index.Add("path/parent-folder");
// Prepare the Regex Query and Search
// Regex here is to identify all words having any consecutive repeated characters.
string query = "^(.)\\1{1,}";
SearchResult result = index.Search(query);
// Highlighting and Printing Regex Search Results for all the documents
for (int i = 0; i < result.DocumentCount; i++)
{
FoundDocument document = result.GetFoundDocument(i);
OutputAdapter outputAdapter = new FileOutputAdapter(OutputFormat.Html, indexFolder + "/Highlight" + i + ".html");
Highlighter highlighter = new DocumentHighlighter(outputAdapter);
index.Highlight(document, highlighter);
Console.WriteLine("\tDocument: " + document.DocumentInfo.FilePath);
Console.WriteLine("\tOccurrences: " + document.OccurrenceCount);
for (int j = 0; j < document.FoundFields.Length; j++)
{
FoundDocumentField field = document.FoundFields[j];
Console.WriteLine("\t\tField: " + field.FieldName);
Console.WriteLine("\t\tOccurrences: " + document.OccurrenceCount);
// Printing found terms
if (field.Terms != null)
{
for (int k = 0; k < field.Terms.Length; k++)
{
Console.WriteLine("\t\t\t" + field.Terms[k].PadRight(20) + field.TermsOccurrences[k]);
}
}
}
Console.WriteLine("===========================================");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment