susvej
(Susanne Vejdemo)
September 28, 2023, 4:11pm
1
I'm playing with the music dataset and trying to get REGEX to work in SPARQL queries.
I can do a simple query like:
prefix : <http://stardog.com/tutorial/>
SELECT ?soloartist
WHERE {
<http://stardog.com/tutorial/ABBA> :member ?soloartist .
}
And predictably get results like this: Screenshot by Lightshot
But any kind of regex search, including "give me any character whatsoever" gives me 0 triples in return.
prefix : <http://stardog.com/tutorial/>
SELECT ?soloartist ?band
WHERE {
?band :member ?soloartist
FILTER ( REGEX(?band, ".*" ) )
}
This is also true when the regex is a perfect match:
FILTER(REGEX(?band, "<http://stardog.com/tutorial/ABBA>", "i"))
or just has one letter that is expected to be in the returned triple
FILTER(REGEX(?band, "A", "i"))
I've double checked my quotation marks. Is there something Stardog specific I'm not understanding?
StevePlace
(Steve Place)
September 28, 2023, 8:26pm
2
Hi Susanne,
REGEXs don't work on URIs, only strings. That's why your second code block is returning no results. If you change it to this, it'll work:
prefix : <http://stardog.com/tutorial/>
SELECT ?soloartist ?band
WHERE {
?band :member ?soloartist
FILTER ( REGEX(str(?band), ".*" ) )
}
You might think this means your third code block should become this:
FILTER(REGEX(str(?band), "<http://stardog.com/tutorial/ABBA>", "i"))
(I say this because this is what I tested initially.) However, when ?band
gets converted to a string, the angle brackets are not included in the result. In other words, str(<http://stardog.com/tutorial/ABBA>)
gives "http://stardog.com/tutorial/ABBA"
. Therefore, your third code block should be:
FILTER(REGEX(?band, "http://stardog.com/tutorial/ABBA", "i"))
Best,
Steve
susvej
(Susanne Vejdemo)
September 29, 2023, 8:45pm
3
Thank you Steve, that makes a lot of sense!