REGEXEXTRACT - Query language in Cosmos DB (in Azure and Fabric)

The REGEXEXTRACT function returns the first substring that matches a regular expression pattern.

An Azure Cosmos DB system function that extracts text that matches a regular expression.

Syntax

REGEXEXTRACT(<subject>, <pattern>, [<options>], [<group_number>])

Arguments

Description
subject The string to search.
pattern The regular expression pattern to match.
options Optional. A string of regular expression flags: i (ignore case), m (multiline), s (singleline or dotall), and x (ignore pattern whitespace).
group_number Optional. The 1-based index of the capture group to return. A value of 0, or omitting this argument, returns the whole match.

Return types

Returns a string expression.

Examples

This section contains examples of how to use this query language construct.

Extract text with a regular expression

In this example, the REGEXEXTRACT function returns the first match from the input string.

SELECT VALUE {
  skuPrefix: REGEXEXTRACT("SKU-93821-BLUE", "[A-Z]+"),
  firstDigits: REGEXEXTRACT("SKU-93821-BLUE", "[0-9]+")
}
[
  {
    "skuPrefix": "SKU",
    "firstDigits": "93821"
  }
]

Extract a capture group from the first match

In this example, the REGEXEXTRACT function uses a capture group and case-insensitive matching to return only the numeric portion of the first SKU.

SELECT VALUE {
  firstSkuNumber: REGEXEXTRACT("sku-93821-blue sku-48152-red", "sku-([0-9]+)-[a-z]+", "i", 1)
}
[
  {
    "firstSkuNumber": "93821"
  }
]

Remarks

  • This function doesn't use the index.