# Regular expression search This article describes regular expression (RegEx) search queries, which are universal and very flexible but can be slow in large indexes. Regular expression (RegEx) search queries are universal and very flexible, but at the same time, in large indexes, their performance can be low, because the regular expression is matched against the words of the index. Another limitation of regular expressions is that search with a pattern is done for each word separately, and not the entire text of a document field. Words are stored in the index in lower case, therefore regular expression search is always case-insensitive. Queries of this type in text form always start with the caret character, and if the regular expression itself also starts with the caret character, then there will be two caret characters at the beginning of the query. A regex search query in text form cannot be combined with other types of queries. See the [Query language specification]({{< ref "search/net/developer-guide/advanced-usage/searching/query-language-specification.md" >}}) page. However, in object form, regular expressions can be combined with other queries. See the [Nesting search queries in object form]({{< ref "search/net/developer-guide/advanced-usage/searching/nesting-search-queries-in-object-form.md" >}}) page. The following example demonstrates the regex search in text and object forms. **C#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Search with a regular expression in text form string query1 = "^^(.)\\1{1,}"; // The first caret character at the beginning indicates that this is a regular expression search query SearchResult result1 = index.Search(query1); // Search for two or more identical characters at the beginning of a word // Search with a regular expression in object form SearchQuery query2 = SearchQuery.CreateRegexQuery("^(.)\\1{1,}"); // Search for two or more identical characters at the beginning of a word SearchResult result2 = index.Search(query2); ``` In object form, a regular expression query can also be created with the [CreateRegexQuery](https://reference.groupdocs.com/search/net/groupdocs.search/searchquery/createregexquery/) method overload that takes a combination of [RegexOptions](https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regexoptions) values as the second argument. The options must contain the RegexOptions.IgnoreCase flag; otherwise, ArgumentException is thrown. The overload with one argument uses RegexOptions.IgnoreCase. **C#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Creating a regular expression query with the specified options RegexOptions regexOptions = RegexOptions.IgnoreCase | RegexOptions.CultureInvariant; SearchQuery query = SearchQuery.CreateRegexQuery("^(.)\\1{1,}", regexOptions); // Search for two or more identical characters at the beginning of a word SearchResult result = index.Search(query); ``` A regular expression that cannot be used, for example an expression that is not valid, finds nothing, and the other parts of the query are searched. The error is reported by the [ErrorOccurred](https://reference.groupdocs.com/search/net/groupdocs.search.events/eventhub/erroroccurred/) event of the index and, since version 26.11, in the [Warnings](https://reference.groupdocs.com/search/net/groupdocs.search.results/searchresult/warnings/) property of the result by a line that starts with "Query part not searched: ". Since version 26.10, regular expression search uses as many threads as there are processor cores by default; the order of the found words in the search result does not depend on the number of search threads (the [SearchThreads](https://reference.groupdocs.com/search/net/groupdocs.search/indexsettings/searchthreads/) index setting). Since version 26.11, the searches of an index that run at the same time share these threads (see the [Search thread safety]({{< ref "search/net/developer-guide/advanced-usage/searching/search-thread-safety.md" >}}) page). Also since version 26.11: * The words that match a regular expression are kept for the next searches with the same query until the index is changed, so a repeated search is faster. * A search is canceled (see the [Search options]({{< ref "search/net/developer-guide/advanced-usage/searching/search-options.md" >}}) page) also while it matches the regular expression with the words of the index. * In a case-sensitive search, a regular expression finds the words in any letter case, and the warnings of the result tell it (see the [Case sensitive search]({{< ref "search/net/developer-guide/advanced-usage/searching/case-sensitive-search.md" >}}) page). ## More resources ### GitHub examples You may easily run the code from documentation articles and see the features in action in our GitHub examples: * [GroupDocs.Search for .NET examples](https://github.com/groupdocs-search/GroupDocs.Search-for-.NET) * [GroupDocs.Search for Java examples](https://github.com/groupdocs-search/GroupDocs.Search-for-Java) ### Free online document search App Along with full featured .NET library we provide simple, but powerful free Apps. You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online [Free Online Document Search App](https://products.groupdocs.app/search).