解析SPARQL结果以获取主机名

use*_*544 0 sparql stardog

我有一个巨大的三元组列表,如下所示:

?s ex:url ?url
Run Code Online (Sandbox Code Playgroud)

哪里?url可以:

www.ex.com/data/1.html
www.ex.com/data/2.html
www.google.com/search
...
Run Code Online (Sandbox Code Playgroud)

使用SPARQL查询是否有可能以某种方式过滤查询并获取不同的域列表?在示例中,www.ex.comwww.google.com.

像这样的东西:

SELECT distinct ?url
WHERE { ?s ex:url ?url }
Run Code Online (Sandbox Code Playgroud)

但治疗每个网址绑定.当然我可以全部获取它们,并在我的程序中逐个处理每个URL,但我认为sparql查询会更有效.我正在使用Stardog - 以防它有一些自定义功能.

Jos*_*lor 5

您可以使用不需要正则表达式的字符串操作来执行此类操作.例如,您可以在"//"之后和"/"之前获取URL的字符串形式的一部分:

select ?url ?hostname {
  values ?url { <http://example.org/index.html> }
  bind(strbefore(strafter(str(?url),"//"),"/") as ?hostname)
}
Run Code Online (Sandbox Code Playgroud)
---------------------------------------------------
| url                             | hostname      |
===================================================
| <http://example.org/index.html> | "example.org" |
---------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

这不使用正则表达式,并且可能比使用正则表达式函数的解决方案更快.

但是,这可能仍然比主机名更多,例如,如果URL类似于http:// username:password@example.org:8080,您将获得用户名:password@example.org:8080,其中不仅仅是主机名.

要更仔细地执行此操作,您需要选择其中一个URI/URL等规范,例如RFC 3986,并查看有关语法组件的部分.该语法的一些相关产品是:

URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

      hier-part   = "//" authority path-abempty
                  / path-absolute
                  / path-rootless
                  / path-empty
Run Code Online (Sandbox Code Playgroud)

权限组件前面有一个双斜杠("//"),并由下一个斜杠("/"),问号("?")或数字符号("#")字符终止,或者由末尾结束的URI.

authority   = [ userinfo "@" ] host [ ":" port ]
Run Code Online (Sandbox Code Playgroud)

我不会完成所有这些(也许使用正则表达式来处理复杂的情况会更有意义),但是从SPARQL结果中获取URI然后使用实际的URI解析库可能是最简单的.获取主机名.这是最可靠的解决方案,因为URI可能非常复杂.