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.

5
  • 1
    What means "input > NodeValue"? Commented Sep 27, 2016 at 0:42
  • 1
    What do you expect, what does not work currently? Commented Sep 27, 2016 at 0: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. Commented Sep 27, 2016 at 0: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. Commented Sep 27, 2016 at 6: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. Commented Sep 27, 2016 at 8:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So if I want an exact match of a string with regex in SPARQL, how can I do it?
@mee why would you want to use a regex for that? Just do str(?foo) = "bar".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.