REGEXMATCH (Azure Cosmos DB)REGEXMATCH (Azure Cosmos DB)

适用于: SQL API

提供正则表达式功能。Provides regular expression capabilities. 正则表达式是简明而灵活的表示法,用于找到文本的模式。Regular expressions are a concise and flexible notation for finding patterns of text. Azure Cosmos DB 使用与 PERL 兼容的正则表达式 (PCRE)Azure Cosmos DB uses PERL compatible regular expressions (PCRE).


RegexMatch(<str_expr1>, <str_expr2>, [, <str_expr3>])  


是要搜索的字符串表达式。Is the string expression to be searched.

是正则表达式。Is the regular expression.

是要与正则表达式一起使用的选定修饰符的字符串。Is the string of selected modifiers to use with the regular expression. 此字符串值是可选的。This string value is optional. 如果要运行不带修饰符的 RegexMatch,可以添加空字符串或完全省略。If you'd like to run RegexMatch with no modifiers, you can either add an empty string or omit entirely.

可以了解在 Perl 中创建正则表达式的语法You can learn about syntax for creating regular expressions in Perl.

Azure Cosmos DB 支持以下四种修饰符:Azure Cosmos DB supports the following four modifiers:

修饰符Modifier 说明Description
m 将要搜索的字符串表达式视为多行。Treat the string expression to be searched as multiple lines. 如果没有此选项,“^”和“$”将在字符串的开头或结尾匹配,而不是在每一行匹配。Without this option, "^" and "$" will match at the beginning or end of the string and not each individual line.
s 允许使用“.”以匹配任何字符,包括换行符。Allow "." to match any character, including a newline character.
i 模式匹配时忽略大小写。Ignore case when pattern matching.
x 忽略所有空白字符。Ignore all whitespace characters.

返回类型Return types

返回一个布尔表达式。Returns a Boolean expression. 如果要搜索的字符串表达式、正则表达式或所选修饰符无效,则返回 undefined。Returns undefined if the string expression to be searched, the regular expression, or the selected modifiers are invalid.


以下简单的 RegexMatch 示例使用一些不同的修饰符检查字符串“abcd”是否匹配正则表达式。The following simple RegexMatch example checks the string "abcd" for regular expression match using a few different modifiers.

SELECT RegexMatch ("abcd", "ABC", "") AS NoModifiers, 
RegexMatch ("abcd", "ABC", "i") AS CaseInsensitive, 
RegexMatch ("abcd", "ab.", "") AS WildcardCharacter,
RegexMatch ("abcd", "ab c", "x") AS IgnoreWhiteSpace, 
RegexMatch ("abcd", "aB c", "ix") AS CaseInsensitiveAndIgnoreWhiteSpace 

下面是结果集。Here is the result set.

        "NoModifiers": false,
        "CaseInsensitive": true,
        "WildcardCharacter": true,
        "IgnoreWhiteSpace": true,
        "CaseInsensitiveAndIgnoreWhiteSpace": true

借助 RegexMatch,可以使用元字符执行更复杂的字符串搜索,而 StartsWith、EndsWith、Contains 或 StringEquals 系统函数则无法做到这一点。With RegexMatch, you can use metacharacters to do more complex string searches that wouldn't otherwise be possible with the StartsWith, EndsWith, Contains, or StringEquals system functions. 下面是一些附加的示例,你可以使用通过 Azure Cosmos DB 查询操场获得的营养数据集来运行这些示例。Here are some additional examples, which you can run using the nutrition data set available through the Azure Cosmos DB Query Playground.


如果需要在正则表达式中使用元字符,并且不希望它具有特殊的含义,则应使用 \ 对该元字符进行转义。If you need to use a metacharacter in a regular expression and don't want it to have special meaning, you should escape the metacharacter using \.

检查说明中包含单词“salt”刚好一次的项:Check items that have a description that contains the word "salt" exactly once:

WHERE RegexMatch (c.description, "salt{1}","")

检查说明中包含 0 到 99 之间的数字的项:Check items that have a description that contain a number between 0 and 99:

WHERE RegexMatch (c.description, "[0-99]","")

检查说明中包含以“S”或“s”开头的四个字母单词的项:Check items that have a description that contain four letter words starting with "S" or "s":

WHERE RegexMatch (c.description, " s... ","i")


如果正则表达式可以分解为 StartsWith、EndsWith、Contains 或 StringEquals 系统函数,则该系统函数将受益于范围索引This system function will benefit from a range index if the regular expression can be broken down into either StartsWith, EndsWith, Contains, or StringEquals system functions.

后续步骤Next steps