2015-05-11 112 views
0

這裏是我的SPARQL查詢:正則表達式SPARQL查詢

String queryString = 
      "SELECT ?URI " 
        + "WHERE{ ?URI ?predicate ?object ." 
        + "FILTER regex(?URI, \"p\", \"i\")}"; 


    Query query = QueryFactory.create(queryString); 
    QueryExecution qexec = QueryExecutionFactory.create(query, model); 
    try 
    { 
     ResultSet results = qexec.execSelect(); 
     while(results.hasNext()) { 
     QuerySolution soln = results.nextSolution(); 
     Resource z = (Resource) soln.getResource("URI"); 
     System.out.println(z.toString()); 
     } 
    finally { 
     qexec.close(); 
    } 

正如你可以看到我想要選擇包含字母的URI資源P. 我用正則表達式過濾器,但二不輸出得到任何東西,甚至沒有空指針異常。

回答

3

當你模式匹配的URI,因爲它不是一個文字,你需要使用str將其轉換爲字符串。 一個過濾這種方式就是使用contains

Select ?uri 
where {?uri ?p?o. 
filter (contains(str(?uri), "p")) 
} 

,或者您可以使用正則表達式:

Select ?uri 
where {?uri ?p?o. 
FILTER regex(str(?uri), "p", "i")} 
} 
+0

你尖端完美的作品 – DistribuzioneGaussiana