0

I'd like to match an input given by user (String) with a value (String) of a specific node existing in rdf file.

I applied the following exact mode for matching (input=NodeValue):

 ...
 FILTER regex (?NodeValue,"userinput$","i").

for this type of matching (input < NodeValue ) I used the following:

...       
 FILTER regex (?NodeValue,".*userinput.*","i").

So, my question is how to set my regex in order to get the type of matching when (input > NodeValue) I mean a query that's returns a list of ?nodeValue subsumed by a given user input.

Eg. if the user enters patagoniaisbeautiful it returns patagonia.

Thank you in advance.

Anis Elloumi
  • 23
  • 1
  • 4
  • 1
    What means "input > NodeValue"? – UninformedUser Sep 27 '16 at 00:42
  • 1
    What do you expect, what does not work currently? – UninformedUser Sep 27 '16 at 00:42
  • 1
    REGEX works on String values. I don't know the type of `?NodeValue`, but it's best-practice to use `STR(?NodeValue)` as first argument of the REGEX function. – UninformedUser Sep 27 '16 at 00:44
  • 1
    It is unclear to me what you mean by `(input < NodeValue)` and `(input > NodeValue)`... in case they are symbols of inclusion, I wonder why one would want to get all subsets of a user input... I mean, if the user enters `patagonia`, do you really want `p`, `pa`, `tag`, `go`, `ni` and so on? Then again, maybe those symbols mean something else entirely. – Nadia Cerezo Sep 27 '16 at 06:01
  • Thank you guys , I explain more what I mean by (input > NodeValue) , A query that returns a list of ?nodeValue subsumed by a given user input. Eg. if the user enters `patagoniaisbeautiful` it returns `patagonia`. – Anis Elloumi Sep 27 '16 at 08:25

1 Answers1

3

To achieve a match where the database value is a substring of your user input, you need to flip your arguments for the regex function around. That way, the actual value in the database is used as the regular expression, and the user input as the string to match it:

FILTER(REGEX("patagoniaisbeautiful", STR(?NodeValue), "i"))

This will succeed if ?NodeValue is "patagonia". Of course it will also match if ?NodeValue is "p", "a", "t", etc.

In fact, given that you are only interested in simple substring matching here, you can simplify this by using the CONTAINS function, instead of the (computationally expensive) REGEX operation. Like so:

FILTER(CONTAINS("patagoniaisbeautiful", LCASE(STR(?NodeValue))))

As an aside: you give an example of doing a regex where the user input is a substring of the database value: ".*userinput.*". The leading and closing .* here are unnecessary. A SPARQL regex match is by definition a substring match.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73