Back to top

    ReQL command: match

    Command syntax

    string.match(regexp) → null/object

    Description

    Match a string against a regular expression. If there is a match, returns an object with the fields:

    • str: The matched string
    • start: The matched string’s start
    • end: The matched string’s end
    • groups: The capture groups defined with parentheses

    If no match is found, returns null.

    Accepts RE2 syntax. You can enable case-insensitive matching by prefixing the regular expression with (?i). See the linked RE2 documentation for more flags.

    The match command does not support backreferences.

    Example: Get all users whose name starts with “A”. Because null evaluates to false in filter, you can use the result of match for the predicate.

    r.table("users").filter(doc -> doc.g("name").match("^A")).run(conn);
    

    Example: Get all users whose name ends with “n.”

    r.table("users").filter(doc -> doc.g("name").match("n$")).run(conn);
    

    Example: Get all users whose name contains “li.”

    r.table("users").filter(doc -> doc.g("name").match("li")).run(conn);
    

    Example: Get all users whose name is “John,” performing a case-insensitive search.

    r.table("users").filter(doc -> doc.g("name").match("(?i)^john$")).run(conn);
    

    Example: Retrieve the domain of a basic email.

    r.expr("name@domain.com").match(".*@(.*)").run(conn);
    

    Result:

    {
        "start": 0,
        "end": 20,
        "str": "name@domain.com",
        "groups": [
            {
                "end": 17,
                "start": 7,
                "str": "domain.com"
            }
        ]
    }
    

    You can then retrieve only the domain with g() and nth.

    r.expr("name@domain.com").match(".*@(.*)").g("groups").nth(0)
     .g("str").run(conn);
    

    Returns domain.com.

    Example: A failure to parse out the domain name will return null.

    r.expr("name[at]domain.com").match(".*@(.*)").run(conn);
    

    Get more help

    Couldn't find what you were looking for?