Search code examples
sqlrdfsparqlvirtuoso

querying RDFs using SPARQL query


Assuming we have a dataset including the following 3 RDF triples. I want to retrieve all these three triples using sparql query.

http://test.com/11/META_C1-INST  predicate1     http://test.com/NO_CONTEXT/META_C0059714-INST
http://test.com/23/META_C1-INST  predicate1     http://test.com/NO_CONTEXT/META_C0059714-INST
http://test.com/43/META_C1-INST  predicate1     http://test.com/NO_CONTEXT/META_C0142817-INST

Here is the query I wrote but I know it is not right:

prefix pred:<http://test.com/>
            select distinct *
            where {pred:META_C1-INST ?p ?o};

How should I say: "Look for any triples with the subject from http://test.com/??/ where ?? can be anything like 11, 23, S23?


Solution

  • You can transform the URI into a string and then filter based on what it starts with. The keyword you need to use is STRSTARTS:

    select distinct *
    where{
    ?s ?p ?o
    filter(STRSTARTS(str(?s), "http://test.com/"))
    }
    

    or you can use regular expressions with the ^ sign:

    select distinct *
    where{
    ?s ?p ?o
    FILTER regex(str(?s), "^http://test.com/") .
    }