Search code examples
sparqlrdfsemantic-webowl

Slash in IRI (SPARQL)


I have a IRI that has a slash in the IRI:

Example triple

:name/name1 :hasCity :City.

I want to find all names that have the slash in them.

I've tried the query:

select ?name {?name ?pred ?obj FILTER regex(str(?name),"/")}

This gives me all names even if they don't have a /

The I tried:

select ?name {?name ?pred ?obj FILTER regex(str(?name),"//")}

this also gives me all names including ones that don't have a /

The I tried:

select ?name {?name ?pred ?obj FILTER regex(str(?name),"\/")}

for that I get the error: Bad escape sequence in a short double-quoted string at '"\'

Is there a right way to get the ones with "/" only in the part after the IRI prefix?

Followup question: Is there a way to query those names without sparql throwing an error?


Solution

  • Most "typical" IRIs will already have at least two slashes in them (e.g., in http://), but not all IRIs are like that (e.g., urn:ex:foo), so there's not going to be a universal way to do this. However, if you're mostly using "typical" IRIs, then you can just use a regular expression that looks for a slash after the double slashes, like "//.*/". For example:

    select ?name {
      values ?name { <urn:ex:foo>             #-- no slash, but won't match
                     <http://example.org>     #-- no slash
                     <http://example.org/foo> #-- a slash
                   }
      filter regex(str(?name), "//.*/")
    }
    
     ----------------------------
    | name                     |
    ============================
    | <http://example.org/foo> |
    ----------------------------