On this page本页内容
$regexMatch
¶New in version 4.2.版本4.2中的新功能。
Performs a regular expression (regex) pattern matching and returns:执行正则表达式(regex)模式匹配并返回:
true
if a match exists.true
。false
if a match doesn’t exist.false
。MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.41 with UTF-8 support.MongoDB使用Perl兼容的正则表达式(即“PCRE”)版本8.41,支持UTF-8。
Prior to MongoDB 4.2, aggregation pipeline can only use the query operator $regex
in the $match
stage. For more information on using regex in a query, see 有关在查询中使用regex的更多信息,请参阅$regex
.$regex
。
The $regexMatch
operator has the following syntax:语法如下所示:
input | The string on which you wish to apply the regex pattern. Can be a string or any valid expression that resolves to a string. | ||||||||||
regex | The regex pattern to apply. Can be any valid expression that resolves to either a string or regex pattern
Alternatively, you can also specify the regex options with the options field. To specify the You cannot specify options in both the | ||||||||||
options | Optional. The following Note
|
The operator returns a boolean:运算符返回一个布尔值:
true
if a match exists.false
if a match doesn’t exist.See also参阅
$regexMatch
and Collation¶$regexMatch
ignores the collation specified for the collection, db.collection.aggregate()
, and the index, if used.
For example, the create a sample collection with collation strength 1
(i.e. compare base character only and ignore other differences such as case and diacritics):
Insert the following documents:
Using the collection’s collation, the following operation performs a case-insensitive and diacritic-insensitive match:使用集合的排序规则,以下操作执行不区分大小写和不区分重音的匹配:
The operation returns the following 3 documents:
However, the aggregation expression $regexMatch
ignores collation; that is, the following regular expression pattern matching examples are case-sensitive and diacritic sensitive:
Both operations return the following:
To perform a case-insensitive regex pattern matching, use the i Option instead. See i Option for an example.
$regexMatch
and Its Options¶To illustrate the behavior of the $regexMatch
operator as discussed in this example, create a sample collection products
with the following documents:
By default, $regexMatch
performs a case-sensitive match. For example, the following aggregation performs a case-sensitive
$regexMatch
on the description
field. The regex pattern /line/
does not specify any grouping:
The operation returns the following:
The following regex pattern /lin(e|k)/
specifies a grouping (e|k)
in the pattern:
The operation returns the following:
i
Option¶Note
You cannot specify options in both the regex
and the options
field.
To perform case-insensitive pattern matching, include the i option as part of the regex field or in the options field:
For example, the following aggregation performs a case-insensitive
$regexMatch
on the description
field. The regex pattern /line/
does not specify any grouping:
The operation returns the following documents:
m
Option¶Note
You cannot specify options in both the regex
and the options
field.
To match the specified anchors (e.g. ^
, $
) for each line of a multiline string, include the m option as part of the regex field or in the options field:
The following example includes both the i
and the m
options to match lines starting with either the letter s
or S
for multiline strings:
The operation returns the following:
x
Option¶Note
You cannot specify options in both the regex
and the options
field.
To ignore all unescaped white space characters and comments (denoted by the un-escaped hash #
character and the next new-line character) in the pattern, include the s option in the options field:
The following example includes the x
option to skip unescaped white spaces and comments:
The operation returns the following:
s
Option¶Note
You cannot specify options in both the regex
and the options
field.
To allow the dot character (i.e. .
) in the pattern to match all characters including the new line character, include the s option in the options field:
The following example includes the s
option to allow the dot character (i.e. .) to match all characters including new line as well as the i
option to perform a case-insensitive match:
The operation returns the following:
$regexMatch
to Check Email Address¶Create a sample collection feedback
with the following documents:
The following aggregation uses the $regexMatch
to check if the comment
field contains an email address with @mongodb.com
and categorize the feedback as Employee
or External
.
The operation returns the following documents: