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

8 投票
1 回答
9464 浏览
提问于 2025-04-16 01:26

我在我的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

1 个回答

15

那这个呢?

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

撰写回答