问题
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 (\b). 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: <http://xmlns.com/foaf/0.1/> "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
+ "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";
recipe 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?
Thanks.
回答1:
You need \\\\W - yes, 4 . One level goes for Java to get \\W into SPARQL, the next level is the SPARQL string to leave exactly \W.
回答2:
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.
来源:https://stackoverflow.com/questions/38601796/sparql-query-regex-boundary-in-java-returns-empty