我想在其主题中提取一个包含单词"alice"的三元组.我使用的查询是:
SELECT ?s ?p ?o WHERE { ?s ?p ?o .FILTER regex(?s, \"alice\") .}
Run Code Online (Sandbox Code Playgroud)
尽管有三重满足这个约束,但这并没有给我任何结果.
另一方面,当我使用相同的查询来提取在其对象中包含单词brillant的三元组时.它仅返回2个可能匹配中的一个.
使用的查询是:
SELECT ?s ?p ?o WHERE { ?s ?p ?o .FILTER regex(?o, \"brillant\") .}
Run Code Online (Sandbox Code Playgroud)
请让我知道我哪里出错了,这种行为的原因是什么.
Jos*_*lor 18
我会假设引号周围的转义只是复制和粘贴的残余.正则表达式的第一个参数必须是文字,但文字不能成为RDF中三元组的主题,因此您拥有的数据不应该与此模式匹配.但是,您可能拥有的主题是URI,其URI包含字符串"alice",您可以使用str函数获取URI的字符串表示形式.例如,
SELECT ?s ?p ?o WHERE { ?s ?p ?o .FILTER regex(str(?s), "alice") .}
Run Code Online (Sandbox Code Playgroud)
为了说明,让我们使用这两个值<http://example.org>
并"string containing example"
像在原始查询中那样进行过滤:
select ?x where {
values ?x { <http://example.org> "string containing example" }
filter( regex(?x, "exam" ))
}
Run Code Online (Sandbox Code Playgroud)
-------------------------------
| x |
===============================
| "string containing example" |
-------------------------------
Run Code Online (Sandbox Code Playgroud)
我们只得到"string containing example"
因为另一个值不是字符串,因此不适合regex
.但是,如果我们将调用添加到str
,那么它是regex将考虑的URI的字符串表示:
select ?x where {
values ?x { <http://example.org> "string containing example" }
filter( regex(str(?x), "exam" ))
}
Run Code Online (Sandbox Code Playgroud)
-------------------------------
| x |
===============================
| <http://example.org> |
| "string containing example" |
-------------------------------
Run Code Online (Sandbox Code Playgroud)