DRSK
DRSK

Reputation: 63

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?

Upvotes: 0

Views: 1139

Answers (3)

Borislav Markov
Borislav Markov

Reputation: 1735

You can escape the slash with a backslash, for example:

PREFIX mdb: <http://example.org/movieDB/>
PREFIX country: <http://example.org/movieDB/country/>
BASE <http://example.org/movieDB/>
construct {
    ?s ?p ?o
} 
where {
    ?s ?p ?o .
    ?s mdb:country mdb:country\/France .
}

Upvotes: 0

Joshua Taylor
Joshua Taylor

Reputation: 85883

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> |
----------------------------

Upvotes: 2

user205512
user205512

Reputation: 8898

Example triple

:name/name1 :hasCity :City.

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 /

This answer is almost certainly correct, but it isn't obvious because you're looking at abbreviated names.

Try the following:

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

That will ensure that the name of the object is displayed as seen by the regex. My guess is that all your objects have URIs starting http:// or similar, and they'll obviously match.

Upvotes: 2

Related Questions