Filtering using a regex

Hey, new to using Dato and need some help getting filtering to work using a regex - trying to pull back matching whole words in single / multi line string fields. Documentation on the subject is a bit thin and can’t find any threads which cover it but apologies up front if I’ve missed an article. Running the search from the API Explorer and using {someTextField: {matches: {pattern: "someValidRegex", caseSensitive: false}}} doesn’t give any results when the clause should return a number of records. Does pattern take a straight regex or does it need additional formatting ? Thanks in advance.

2 Likes

Hello @jonathan.allen

Could you send us the exact query and the project you are using it on at support@datocms.com so that we can take a closer look?

Thanks!

Hey @m.finamor , thanks for the quick reply.

The project is 48590, running against develop

The query is:

query AllAdverts { allAdverts(first: 100, filter: {OR: [ {offerPromotionalText: {matches: {pattern: "\bhat\b", caseSensitive: false}}}, {advertiserAboutUs: {matches: {pattern: "\bhat\b", caseSensitive: false}}}, {advertiserName: {matches: {pattern: "\bhat\b", caseSensitive: false}}},], approvalStatus: {eq: "Approved"}}) { advertiserName advertiserAboutUs offerPromotionalText } }

The actual query is constructed in a web app based on user search terms so need to match whole words only. Tried a lot of variations but no joy. The expression is fine running it on regex101 with appropriate samples.

Thanks in advance.

1 Like

Hello @jonathan.allen

For word boundaries we should use \y instead of \b, and we should use double backslashes to escape the character.
However, there is no record that matches the word “hat” in your project, there is one that has the word “that” in one of the “offerPromotionalText”
So the query

query AllAdverts {
  allAdverts(first: 100, filter: {OR: [{offerPromotionalText: {matches: {pattern: "\\ythat\\y", caseSensitive: false}}}, {advertiserAboutUs: {matches: {pattern: "\\ythat\\y", caseSensitive: false}}}, {advertiserName: {matches: {pattern: "\\ythat\\y", caseSensitive: false}}}], approvalStatus: {eq: "Approved"}}) {
    advertiserName
    advertiserAboutUs
    offerPromotionalText
  }
}

Returns that record

1 Like

Thanks @m.finamor, that solves it. There are 2 records on the develop environment which have “hat” in their advertiserName field and they pull back fine and those which have “that” are not returned.

Interesting that \b is not supported. Is there any documentation to cover what is supported and what isn’t ?

Thanks.

Hello, I have similar problem with filtering posts (allProducts in my case)

I need to get lists of products that cointains all of these words "bmw, F20, red, stripe

with this query ?

query MyQuery {
  allProducts(filter: {name: {matches: {pattern: "
^(?=.*\bbmw\b)(?=.*\bf20\b)(?=.*\bred\b)(?=.*\bstripe\b).+
"}}}) {
    name
  }
}

can somebody help me ???

Hello @krzysztof.bialek1 sorry for the delay on this

It seems like the regex you are looking for is

^(?=.*bmw)(?=.*F20)(?=.*Red)(?=.*Stripe).*

Using positive lookahead to search for the 4 words “bmw” “F20”, "Red, and “Stripe”

yes! that’s a good answer :slight_smile: thank you !

1 Like