Fuzzy search on emails with @ symbol - escape character needed?

5 replies
Last updated: Nov 30, 2020
Hi, i am trying to do a fuzzy search on emails e.g.
email match "*@foo.com"
but it doesn't seem to work with the @. Is there a special escape char?
AI Update

The match operator in GROQ isn't designed for pattern matching with special characters like @ - it's actually a full-text search operator that tokenizes text, which means it treats special characters as word separators.

When you have an email like user@foo.com, the match operator tokenizes it into separate terms: ['user', 'foo', 'com']. So the @ symbol is stripped out during indexing, which is why your pattern "*@foo.com" doesn't work as expected.

What you can do instead:

For domain-based email matching, use string functions or pattern matching:

// This will match emails with foo.com domain (but not perfectly)
*[email match "foo.com"]  // Matches because "foo" and "com" are tokens

// More precise: use string functions for exact matching
*[string::split(email, "@")[1] == "foo.com"]

For checking if email contains a substring, you can use the in operator:

// Check if email contains the domain
*["@foo.com" in email]

For prefix matching on the full string, use string::startsWith():

// Match emails starting with a specific prefix
*[string::startsWith(email, "admin@")]

The key takeaway: match is meant for free-text search (like searching article content), not for pattern matching with special characters. For email domain filtering or any exact string pattern matching, you'll want to use GROQ's string functions or comparison operators instead. There's no escape character because the operator fundamentally works differently than regex-style pattern matching.

Show original thread
5 replies
I've asked the GROQ team about how the wildcard and match operator work, in the meantime you can probably get by with matching against
<http://foo.com|foo.com>
, so
email match "<http://foo.com|foo.com>"
or
email match "*<http://foo.com|foo.com>"
. I'll chime back once I know more though.
I've asked the GROQ team about how the wildcard and match operator work, in the meantime you can probably get by with matching against
<http://foo.com|foo.com>
, so
email match "<http://foo.com|foo.com>"
or
email match "*<http://foo.com|foo.com>"
. I'll chime back once I know more though.
Got some clarification here:
match is a free text search, not a string filtering operator and will not support arbitrary strings with special characters predictably. "<mailto:banana@sanity.io|banana@sanity.io>" will be indexed as ['banan', 'sanity', 'io']. The best way to do what you want here is to match "sanity" and then post-filter your result using a regex on the client side.

We'll try to update the docs about this.
Thanks!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?