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 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)) {

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

(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)) {

How can I get it working?


(Michel Conrado (Support Engineer)) #4

Like this:

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

You can also run this query in as it is.

(Freddiewanah) #5

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

Thanks a lot.