W3cubDocs

/RethinkDB Ruby

ReQL command: match

Command syntax

string.match(regexp) → nil/object

Description

Matches 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 nil.

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 nil evaluates to false in filter, you can just use the result of match for the predicate.

r.table('users').filter{ |doc|
    doc['name'].match("^A")
}.run(conn)

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

r.table('users').filter{ |doc|
    doc['name'].match("n$")
}.run(conn)

Example: Get all users whose name has “li” in it

r.table('users').filter{ |doc|
    doc['name'].match("li")
}.run(conn)

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

r.table('users').filter{ |doc|
    doc['name'].match("(?i)^john$")
}.run(conn)

Example: Get all users whose name is composed of only characters between “a” and “z”.

r.table('users').filter{ |doc|
    doc['name'].match("(?i)^[a-z]+$")
}.run(conn)

Example: Get all users where the zipcode is a string of 5 digits.

r.table('users').filter{ |doc|
    doc['zipcode'].match("\\d{5}")
|.run(conn)

Example: Retrieve the domain of a basic email

r.expr("[email protected]").match(".*@(.*)").run(conn)

Result:

{
    :start => 0,
    :end => 20,
    :str => "[email protected]",
    :groups => [
        {
            :end => 17,
            :start => 7,
            :str => "domain.com"
        }
    ]
}

You can then retrieve only the domain with the [] selector.

r.expr("[email protected]").match(".*@(.*)")["groups"][0]["str"].run(conn)

Returns 'domain.com'

Example: Fail to parse out the domain and returns nil.

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

Related commands

Get more help

Couldn't find what you were looking for?

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/ruby/match/