Jeje Jeje - 1 year ago 98
Java Question

SPARQL query REGEX boundary in java returns empty

i'm new in SPARQL and Jena. Here i'm using Jena version 2.13. I would like to perform a SPARQL query from java web application with REGEX boundary

. I'm using REGEX boundary to match and find exact word.

I'm running the same SPARQL query on protege too.

Here my SPARQL query in java :

"PREFIX foaf: <> "
+ "PREFIX rdf: <> "
+ "PREFIX owl: <> "
+ "PREFIX xsd: <> "
+ "PREFIX rdfs: <> "
+ "PREFIX : <" + CROSS_NS + "> "
+ "SELECT ((?FoodAndBeveragesRecipes) AS ?Resep)((?FoodAndBeverages) AS ?a) "
+ "WHERE{ ?FoodAndBeveragesRecipes :IngR ?_IngR."
+ " ?_IngR :HasIngredients ?FoodAndBeverages;"
+ ":UnitValue ?Value;"
+ " :MeasurementUnit ?Measurements;"
+ " :Ing_Type ?IngType."
+ "FILTER regex(str(?FoodAndBeverages), \"\\b" + recipe + "\\b\")}"
+ " ORDER BY ?FoodAndBeveragesRecipes";

is String variabel to store user input, it's received as a parameter.

When i perform the query, Jena returns an empty set (not even null pointer exception) while Protégé return/find the correct results.
I don't know if the problem is with the REGEX code or what.

I was wondering why my REGEX is not working correctly ? could anyone help me?


Answer Source

Word boundaries are not included in the SPARQL regex flavor (which is the same as XPath flavor). Perhaps Protégé supports them as an extension, but Jena doesn't. Try using (^|\W) and (\W|$) instead:

+ "FILTER regex(str(?FoodAndBeverages), \"(^|\\\\W)" + recipe + "(\\\\W|$)\")}"    

The extra backslashes are there because the string has two layers of escaping: the Java string literal, then the SPARQL interpreter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download