sparql-提取URI的最后一部分

发布于 2025-02-13 08:00:11 字数 647 浏览 1 评论 0原文

我有来自不同域的URI列。例如,

http://comicmeta.org/cbo/category
http://purl.org/dc/terms/hasVersion
http://schema.org/contributor

依此类推。我想在每个此类URI上的最后一个斜杠'/'之后提取最后一部分,即,字符串。

上面的URIS列表中的预期结果:

category
hasVersion
contributor

如何编写 generic sparql查询以从任何给定的URI中提取最后一部分?

这就是我到目前为止尝试的:

SELECT distinct ?s ?x WHERE { 
    ?s ?p ?o .
    BIND (STRBEFORE(STRAFTER(STR(?s),"/"), " ") as ?x) .
    #To extract the part after the slash '/' and before the end of string indicated by a space ' '. 

}

但是,这只返回空字符串“”。

我该如何完成这项工作?有人可以帮我吗?

I have a column of URIs from different domains. Example,

http://comicmeta.org/cbo/category
http://purl.org/dc/terms/hasVersion
http://schema.org/contributor

and so on. I want to extract the last part, i.e, the string after the last slash '/' on each such URI.

Expected results on the above list of URIs:

category
hasVersion
contributor

How do I write a generic SPARQL query to extract this last part from any given URI?

This is what I have tried so far:

SELECT distinct ?s ?x WHERE { 
    ?s ?p ?o .
    BIND (STRBEFORE(STRAFTER(STR(?s),"/"), " ") as ?x) .
    #To extract the part after the slash '/' and before the end of string indicated by a space ' '. 

}

But, this only returns empty strings "".

How can I make this work? Can someone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2025-02-20 08:00:11

使用替换是这样:

BIND (REPLACE(STR(?s), "^.*/([^/]*)$", "$1") as ?x)

这仅在最后一个/字符之后找到的零件代替整个字符串。但是请注意,由于您的示例,并非所有词汇量都使用/作为定界符;有些还使用http://www.w3.org/1999/02/02/22-rdf-syntax-ns#type将变成22-rdf-syntax-ns#代码>

如果您不想要它,则可以使用一些更复杂的东西:

BIND (REPLACE(STR(?s), "^.*?([_\\p{L}][-_\\p{L}\\p{N}]*)$", "$1") as ?x)

这通常是根据通常是有效的XML名称来选择最长的部分。

Using REPLACE is the way:

BIND (REPLACE(STR(?s), "^.*/([^/]*)
quot;, "$1") as ?x)

This replaces the whole string with only the part found after the last / character. Note however that, due to your examples, not all vocabularies use / as the delimiter; some also use #. Something like http://www.w3.org/1999/02/22-rdf-syntax-ns#type will be turned into 22-rdf-syntax-ns#type

If you do not want that, you could use something a bit more complicated:

BIND (REPLACE(STR(?s), "^.*?([_\\p{L}][-_\\p{L}\\p{N}]*)
quot;, "$1") as ?x)

This selects the longest part from the end based on what usually is a valid XML name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文