A newer version of this documentation is available.

View Latest
March 16, 2025
+ 12

Pattern-matching functions allow you to find regular expression patterns in strings or attributes. Regular expressions can formally represent various string search patterns using different special characters to indicate wildcards, positional characters, repetition, optional or mandatory sequences of letters, etc. N1QL functions are available to find matching patterns, find position of matching pattern, or replace a pattern with a new string.

For more information on all supported REGEX patterns, see https://golang.org/pkg/regexp/syntax.

Couchbase Server 4.x N1QL supports regular expressions supported by The Go Programming Language version 1.4.2. From Couchbase Server 5.0, The Go Programming Language version 1.8 is supported.

REGEXP_CONTAINS(expression, pattern)

This function has an alias REGEX_CONTAINS().

Arguments

expression

String, or any N1QL expression that evaluates to a string.

pattern

String representing a supported regular expression.

Return Value

Returns TRUE if the string value contains any sequence that matches the regular expression pattern.

Example

Example 1.
N1QL
SELECT name FROM `travel-sample`.inventory.landmark WHERE REGEXP_CONTAINS(name, "In+.*") LIMIT 5;
Results
json
[ { "name": "Beijing Inn" }, { "name": "Sportsman Inn" }, { "name": "In-N-Out Burger" }, { "name": "Mel's Drive-In" }, { "name": "Inverness Castle" } ]

REGEXP_LIKE(expression, pattern)

This function has an alias REGEX_LIKE().

Arguments

expression

String, or any N1QL expression that evaluates to a string.

pattern

String representing a supported regular expression.

Return Value

Returns TRUE if the string value exactly matches the regular expression pattern.

Example

Example 2.
N1QL
SELECT name FROM `travel-sample`.inventory.landmark WHERE REGEXP_LIKE(name, "In+.*") LIMIT 5;
Results
json
[ { "name": "In-N-Out Burger" }, { "name": "Inverness Castle" }, { "name": "Inverness Museum & Art Gallery" }, { "name": "Inverness Botanic Gardens" }, { "name": "International Petroleum Exchange" } ]

REGEXP_MATCHES(expression, pattern)

Arguments

expression

String, or any N1QL expression that evaluates to a string.

pattern

String representing a supported regular expression.

Return Value

Returns an array of all substrings matching the expression pattern within the input string expression. Returns an empty array if no match is found.

Examples

Example 3.

The following query finds all words beginning with upper or lower case B.

N1QL
SELECT REGEXP_MATCHES("So, 'twas better Betty Botter bought a bit of better butter", "\\b[Bb]\\w+"); (1)
Results
json
[ { "$1": [ "better", "Betty", "Botter", "bought", "bit", "better", "butter" ] } ]
1 The backslash that introduces an escape sequence in the regular expression must itself be escaped by another backslash in the N1QL query. So \b (word boundary) must be entered as \\b and \w (word character) must be entered as \\w.
Example 4.

The following query finds sequences of two words beginning with upper or lower case B.

N1QL
SELECT REGEXP_MATCHES("So, 'twas better Betty Botter bought a bit of better butter", "\\b[Bb]\\w+ \\b[Bb]\\w+");
Results
json
[ { "$1": [ "better Betty", "Botter bought", (1) "better butter" ] } ]
1 Note that Betty Botter is not found in this example, because Betty has already been found by the first match.

REGEXP_POSITION(expression, pattern)

This function has an alias REGEX_POSITION().

Arguments

expression

String, or any N1QL expression that evaluates to a string.

pattern

String representing a supported regular expression.

Return Value

Returns first position of the occurrence of the regular expression pattern within the input string expression. Returns -1 if no match is found. Position counting starts from zero.

Example

Example 5.

The following query finds positions of first occurrence of vowels in each word of the name attribute.

N1QL
SELECT name, ARRAY REGEXP_POSITION(x, "[aeiou]") FOR x IN TOKENS(name) END FROM `travel-sample`.inventory.hotel LIMIT 2;
Results
json
[ { "$1": [ 1, 1, 1 ], "name": "Medway Youth Hostel" }, { "$1": [ 2, 1, 1 ], "name": "The Balmoral Guesthouse" } ]

Note that the order of tokens in the second result may be different.

REGEXP_REPLACE(expression, pattern, repl [, n])

This function has an alias REGEX_REPLACE().

Arguments

expression

String, or any N1QL expression that evaluates to a string.

pattern

String representing a supported regular expression.

repl

String, or any N1QL expression that evaluates to a string.

n

[Optional] The maximum number of times to find and replace the matching pattern.

Return Value

Returns new string with occurrences of pattern replaced with repl. If n is given, at the most n replacements are performed. If n is not provided, all matching occurrences are replaced.

Examples

Example 6.
N1QL
SELECT REGEXP_REPLACE("N1QL is Sql (in fact, sql++) for NoSql", "[sS][qQ][lL]", "SQL"), REGEXP_REPLACE("Winning innings Inn", "[Ii]n+", "Hotel", 6), REGEXP_REPLACE("Winning innings Inn", "[IiNn]+g", upper("inning"), 2);
Results
json
[ { "$1": "N1QL is SQL (in fact, SQL++) for NoSQL", "$2": "WHotelHotelg HotelHotelgs Hotel", "$3": "WINNING INNINGs Inn" } ]
Example 7.

In this example, the query retrieves first 4 documents and replaces the pattern of repeating n with emphasized ‘NNNN’.

N1QL
SELECT name, REGEXP_REPLACE(name, "n+", "NNNN") as new_name FROM `travel-sample` LIMIT 4;
Results
json
[ { "name": "40-Mile Air", "new_name": "40-Mile Air" }, { "name": "Texas Wings", "new_name": "Texas WiNNNNgs" }, { "name": "Atifly", "new_name": "Atifly" }, { "name": "Jc royal.britannica", "new_name": "Jc royal.britaNNNNica" } ]

REGEXP_SPLIT(expression, pattern)

Arguments

expression

String, or any N1QL expression that evaluates to a string.

pattern

String representing a supported regular expression.

Return Value

Returns an array of all the substrings created by splitting the input string expression at each occurrence of the expression pattern. Returns an empty array if no match is found.

Example

Example 8.
N1QL
SELECT REGEXP_SPLIT("C:\\Program Files\\couchbase\\server\\bin", "[\\\\]") AS Windows, (1) REGEXP_SPLIT("/opt/couchbase/bin", "/") AS Unix;
Results
json
[ { "Unix": [ "", (2) "opt", "couchbase", "bin" ], "Windows": [ "C:", "Program Files", "couchbase", "server", "bin" ] } ]
1 The regular expression [\\\\] matches the escaped backslash \\.
2 The REGEXP_SPLIT function returns any zero-length matches that occur at the start of the expression string, or immediately after a previous match.

Aliases

Some pattern-matching functions have an alias whose name begins with REGEX_.