如何在 SPARQL regex() 中使用逻辑 OR?

发布于 2024-09-10 07:10:05 字数 854 浏览 2 评论 0原文

我在 python 程序的 SPARQL 查询中使用这一行:(

FILTER regex(?name, "%s", "i" )

其中 %s 是用户输入的搜索文本)

我希望它匹配 ?name?featurename 包含 %s,但我似乎找不到任何使用 regex() 的文档或教程。我尝试了一些看起来合理的事情:

FILTER regex((?name | ?featurename), "%s", "i" )
FILTER regex((?name || ?featurename), "%s", "i" )
FILTER regex((?name OR ?featurename), "%s", "i" )
FILTER regex((?name, ?featurename), "%s", "i" )

并且每一个都没有 ()

FILTER regex(?name, "%s", "i" ) || regex(?featurename, "%s", "i" )

这样做的正确方法是什么? 谢谢

更新:使用 UNION 有效。但我发现,如果你只是像这样重复 regex() 部分,它也可以工作:

FILTER (regex(?name, "%s", "i") || regex(?featurename, "%s", "i" ))

两种解决方案似乎都有点混乱,因为你必须使用带有相同字符串副本的 2 元素元组来填充两个 %s。

I'm using this line in a SPARQL query in my python program:

FILTER regex(?name, "%s", "i" )

(where %s is the search text entered by the user)

I want this to match if either ?name or ?featurename contains %s, but I can't seem to find any documentation or tutorial for using regex(). I tried a couple things that seemed reasonable:

FILTER regex((?name | ?featurename), "%s", "i" )
FILTER regex((?name || ?featurename), "%s", "i" )
FILTER regex((?name OR ?featurename), "%s", "i" )
FILTER regex((?name, ?featurename), "%s", "i" )

and each of those without the ()

FILTER regex(?name, "%s", "i" ) || regex(?featurename, "%s", "i" )

What's the right way to do this?
Thanks

UPDATE: Using UNION works. But I figured out that it also works if you just repeat the regex() part like so:

FILTER (regex(?name, "%s", "i") || regex(?featurename, "%s", "i" ))

Both solutions seem a little messy in that you have to use a 2-element tuple with copies of the same string to fill in both %ss.

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

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

发布评论

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

评论(1

十秒萌定你 2024-09-17 07:10:05

这又如何呢?

SELECT ?thing
WHERE {
  { 
    ?thing x:name ?name .
    FILTER regex(?name, "%s", "i" )
  } UNION {
    ?thing x:featurename ?name .
    FILTER regex(?featurename, "%s", "i" )
  }
}

What about this?

SELECT ?thing
WHERE {
  { 
    ?thing x:name ?name .
    FILTER regex(?name, "%s", "i" )
  } UNION {
    ?thing x:featurename ?name .
    FILTER regex(?featurename, "%s", "i" )
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文