1

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 books 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.

laalto
  • 150,114
  • 66
  • 286
  • 303
Archana
  • 237
  • 3
  • 9
  • 17
  • Duplicated on http://www.semanticoverflow.com/questions/3750/how-to-match-an-exact-word-through-sparql – RobV Mar 22 '11 at 09:42

2 Answers2

3

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\")}";
laalto
  • 150,114
  • 66
  • 286
  • 303
1

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. "

danja
  • 1,030
  • 2
  • 10
  • 15
  • I need to try this. But for now my input if C++ is typed the program takes C so I need to figure this. I just figured this was the reason as to why C++ was not being searched. Will get back to this solution once my input is correctly taken. – Archana Mar 30 '11 at 07:52