id: search-text-in-html-documents
url: parser/net/search-text-in-html-documents
title: Search text in HTML documents
weight: 2
description: “To search a keyword in HTML documents Search(String) method is used. This method returns the collection of SearchResult objects.”
keywords: search a keyword, search a keyword in HTML
productName: GroupDocs.Parser for .NET
hideChildren: False
toc: true
To search a keyword in HTML documents Search(String) method is used. This method returns the collection of SearchResult objects. For details, see Search Text.
Warning
Parsing does not execute JavaScript or fetch external content; for dynamically generated HTML, pre-render or extract the actual text content before searching.
Here are the steps to search a keyword in HTML document:
Instantiate Parser object for the initial document;
Iterate through the collection and get the position and text.
Warning
Search(String) method returns null value if search isn’t supported for the document. For example, text extraction isn’t supported for Zip archive. Therefore, for Zip archive Search(String) method returns null. For empty HTML document Search(String) method returns an empty collection.
The following example shows how to find a keyword in HTML document:
// Create an instance of Parser classusing(Parserparser=newParser(filePath)){// Search a keyword:IEnumerable<SearchResult>sr=parser.Search("page number");// Iterate over search resultsforeach(SearchResultsinsr){// Print an index and found text:Console.WriteLine(string.Format("At {0}: {1}",s.Position,s.Text));}}
Search(String, SearchOptions) is used for the advanced search in HTML documents - like search with regular expressions. SearchOptions parameter is used to customize a search.
Here are the steps to search with a regular expression in HTML document:
Instantiate Parser object for the initial document;
Instantiate SearchOptions object with the parameters for the search;
Iterate through the collection and get the position and text.
The following example shows how to search with a regular expression in HTML document:
// Create an instance of Parser classusing(Parserparser=newParser(filePath)){// Search with a regular expression with case matchingIEnumerable<SearchResult>sr=parser.Search("page number: [0-9]+",newSearchOptions(true,false,true));// Iterate over search resultsforeach(SearchResultsinsr){// Print an index and found text:Console.WriteLine(string.Format("At {0}: {1}",s.Position,s.Text));}}
Null vs. Empty Collection Results
The Search() method behaves differently depending on the document type and its text content:
Returns null → when the document type does not support searching (e.g., ZIP archives, images, or other non-text formats).
Returns an empty collection → when the document supports searching (like HTML) but contains no searchable text.
Example 1: Search in a non-searchable document (returns null)
using(Parserparser=newParser("sample.zip"))// ZIP archives are not searchable{varresults=parser.Search("example");if(results==null)Console.WriteLine("Search returned null: document type does not support searching.");}
Example 2: Search in an empty HTML file (returns empty collection)
using(Parserparser=newParser("empty.html"))// Empty HTML without text{varresults=parser.Search("example");if(results!=null&&results.Count==0)Console.WriteLine("Search returned empty collection: no text found in the document.");}
Example 3: Search in a valid HTML file (returns results)
using(Parserparser=newParser("sample.html"))// Contains text{varresults=parser.Search("example");if(results!=null){foreach(varresultinresults)Console.WriteLine($"Found: '{result.Text}' at position {result.Position}");}}
Preprocessing Recommendations
GroupDocs.Parser works with the raw text content of HTML documents. It does not execute JavaScript or load external resources (such as CSS, images, or scripts). If your HTML file relies on JavaScript to generate content dynamically, the parser may return an empty collection.
To improve results, consider the following preprocessing steps:
1. Use Pre-rendered HTML
Ensure that the HTML file you provide already contains the visible text content. If your HTML is generated by a web application, save or export the fully rendered version of the page before passing it to the parser.
2. Remove Unnecessary Markup
If possible, simplify your HTML by removing scripts, styles, or metadata that do not affect the visible text. This reduces noise and makes search results more accurate.
3. Normalize Encodings
Verify that the HTML file uses a standard encoding (such as UTF-8). Unexpected or inconsistent encodings may cause missing or unreadable text when searching.
4. Validate Text Presence
If search results are empty, open the HTML file in a text editor to confirm whether actual text is present in the markup. If the content only appears after running JavaScript in a browser, it will not be searchable.
Summary: Provide static, text-based HTML whenever possible. Dynamic or script-driven HTML will not yield results unless converted into plain text or pre-rendered before parsing.
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.