Use parameters in regexp


(Freddiewanah) #1

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.


(Michel Conrado (Support Engineer)) #2

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
https://golang.org/pkg/regexp/syntax/


(Freddiewanah) #3

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


(Michel Conrado (Support Engineer)) #4

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.


(Freddiewanah) #5

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

Thanks a lot.