regex – Tools for representing MongoDB regular expressions
New in version 2.7.
Tools for representing MongoDB regular expressions.
This class is useful to store and retrieve regular expressions that areincompatible with Python’s regular expression dialect.
Parameters:
- pattern: string
- flags: (optional) an integer bitmask, or a string of flagcharacters like “im” for IGNORECASE and MULTILINE
Note that in Python 3, a regular expression compiled from astr
has the re.UNICODE
flag set. If it is undesirableto store this flag in a BSON regular expression, unset it first:
- >>> pattern = re.compile('.*')
- >>> regex = Regex.from_native(pattern)
- >>> regex.flags ^= re.UNICODE
- >>> db.collection.insert({'pattern': regex})
Parameters:
- _regex_: A regular expression object from <code>re.compile()</code>.
Warning
Python regular expressions use a different syntax and differentset of flags than MongoDB, which uses PCRE. A regularexpression retrieved from the server may not compile inPython, or may match a different set of strings in Python thanwhen used in a MongoDB query.
try_compile
()- Compile this
Regex
as a Python regular expression.
Warning
Python regular expressions use a different syntax and differentset of flags than MongoDB, which uses PCRE. A regularexpression retrieved from the server may not compile inPython, or may match a different set of strings in Python thanwhen used in a MongoDB query. try_compile()
may raisere.error
.