Use parameters in regexp

Hi, I was trying to use parameters in the regular expression like this:

regexp(Alias, /^$alias.*$/i)

But it seems returns nothing. However, if I just do a simple search like:

regexp(Alias, /$alias/i)

it works.

SO I’m wondering how should I inject the parameter in the first query.

Hi Freddie,

Why are you using the dollar sign in the beginning?

Test on https://play.dgraph.io/ any example and define the reasons for the expression. Explain what you want to do.

e.g (that you can use on play Dgraph):

{
  directors(func: regexp(name@en, /^steven.*$/i)) {
    name@en
  }
}

OneLine // treat ^ and $ as only matching at beginning and end of text
WasDollar // regexp OpEndText was $, not \z
syntax package - regexp/syntax - Go Packages

Hi Michel,

Thanks for the reply.

Because $alias is the valuable that I pass into the query.

query test($dir: string = "steven"){
  directors(func: regexp(name@en, /^$dir.*$/i)) {
    name@en
  }
}

How can I get it working?

regards

Like this:

query test($name: string = "/^Steven.*$/i") {
  directors(func: regexp(name@en, $name)) {
    name@en
  }
}

You can also run this query in https://play.dgraph.io/ as it is.

Oh, you’re right. I haven’t thought about it this way.

Thanks a lot.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.