Why isn't the regex in my query returning any results?

Created by Steve Place, Modified on Fri, 06 Oct 2023 at 09:36 AM by Steve Place

When using regexes in SPARQL, a common cause of error is to search for URIs rather than strings. Using a regex over a URI is not possible in SPARQL, so you would convert a query like this:

prefix : <http://stardog.com/tutorial/>

SELECT *
WHERE {
   ?band :member ?soloartist 
    FILTER ( REGEX(?band, ".*" ) )
}

to this:

prefix : <http://stardog.com/tutorial/>

SELECT ?soloartist ?band
WHERE {
   ?band :member ?soloartist 
    FILTER ( REGEX(str(?band), ".*" ) )
}

Once this issue is solved, a common pitfall is to include angle brackets in the regex pattern, like this:

FILTER(REGEX(str(?band), "<http://stardog.com/tutorial/The_Beatles>"))

The problem with this is that the angle brackets are not included in the string representation of a URI. Instead, you would filter for The Beatles like so:

FILTER(REGEX(str(?band), "http://stardog.com/tutorial/The_Beatles"))

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article