My application needs to match the correct word i.e. say C or C++ with SPARQL. I have the following SPARQL query
String prolog = "PREFIX kb: <" + VBOOK.getURI() + ">";
String queryString = prolog
+ "\n"
+ "SELECT * "
+ "WHERE {?x kb:Price ?price. ?x kb:Currency ?currency. ?x kb:Title ?title. ?x kb:Count ?count. "
+ "OPTIONAL {?x kb:Description ?description}."
+ "FILTER regex(?title, \"" +title + "\")}";
This matches me all books even C++,C# and b开发者_C百科ooks like -Real Concepts in Embedded Systems (because of the word 'Concepts' having 'C').
if I change the last line to
+ "FILTER regex(?title, \"" +"^"+title + "\")}";
and if title is for example 'C' then my query matches only those books whose titles start with letter 'C'.So books like 'Programming in ANSI C' are not selected.
I have also tried with the following change in the last line
+ "FILTER regex(str(?title), \"" +title + "\")}";
Even this has not helped me much.
How do I modify to get the books pertaining to 'C' even if the books title do not start with C?
Regards, Archana.
This question boils down to what regular expression features your query engine supports. If it supports \b
word boundary matching, you can solve your problem by changing the last line to
+ "FILTER regex(?title, \"\\b" + title + "\\b\")}";
Do you need regex at all?
Instead of making title a variable, simply fix it so -
?x kb:Title "title" .
i.e. in your code
String title = "C++";
"WHERE {?x kb:Price ?price. ?x kb:Currency ?currency. ?x kb:Title \""+title+"\" . ?x kb:Count ?count. "
精彩评论