Related Article(s):
Last active
November 24, 2023 15:12
-
-
Save GroupDocsGists/077a13822324a5341ed40df6985f1c98 to your computer and use it in GitHub Desktop.
Search in Files across Folders by Regex using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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