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.
The regular expression is evaluated by .NET (System.Text.RegularExpressions), whose syntax matches Python’s re module for common patterns.
The following example demonstrates the regex search in text and object forms. The expression is matched against the words of every field of a document, including the creation and modification dates of the file, so a time such as 18:00 adds the word “00” to the words found.
importosfromgroupdocs.searchimportIndex,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search with a regular expression in text formquery1="^^(.)\\1{1,}"# The first caret character at the beginning indicates that this is a regular expression search queryresult1=index.search(query1)# Search for two or more identical characters at the beginning of a word# Search with a regular expression in object formquery2=SearchQuery.create_regex_query("^(.)\\1{1,}")# Search for two or more identical characters at the beginning of a wordresult2=index.search(query2)# Printing the words found in each documentprint("Documents found by the text query:",result1.document_count)print("Documents found by the object query:",result2.document_count)fordocumentinresult2:print(" ",os.path.basename(document.document_info.file_path),document.terms)
Documents found by the text query: 3
Documents found by the object query: 3
library-stats.txt ['00', '550']
quantum-theory.txt ['00']
zoo-guide.txt ['00', 'aardvark', 'eel', 'llama']
In object form, a regular expression query can also be created with the create_regex_query method overload that takes a combination of RegexOptions values as the second argument. The options must contain RegexOptions.IGNORE_CASE; otherwise, ValueError is raised. The overload with one argument uses RegexOptions.IGNORE_CASE. Python’s re flags are accepted too and translated: re.IGNORECASE means RegexOptions.IGNORE_CASE.
importosfromgroupdocs.searchimportIndex,RegexOptions,SearchQuery# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Creating a regular expression query with the specified optionsregex_options=RegexOptions.IGNORE_CASE|RegexOptions.CULTURE_INVARIANTquery=SearchQuery.create_regex_query("^(.)\\1{1,}",regex_options)# Search for two or more identical characters at the beginning of a wordresult=index.search(query)# Printing the words found in each documentprint("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path),document.terms)
Since version 26.10, regular expression search uses as many threads as there are processor cores by default. Therefore, the order of the found words in the search result may differ from run to run. To get the same order every time, set the search_threads index setting to NumberOfThreads.ONE.
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: