Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sparql – How to specifiy Property paths with regex

Imagine I would like to query all descendants of Otto Bismarck until generation 3.
How could I write the sparql code with regex? In this tutorial it says that we can use regex but I don’t know how.

I tried to use "{3}":

SELECT ?descendant ?descendantLabel
WHERE
{
  wd:Q8442 wdt:P40{3} ?descendant.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} 

However, this does not work. The output should be this:
try here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s not possible to write queries with REGEX, but the REGEX syntax can look similar to property paths, hence why you might have been confused.

As for writing paths of length of up to 3, the syntax you are using did not actually make it in the standard, even though it does appear in a few documents.

I’d use something like:

SELECT DISTINCT ?descendant ?descendantLabel
WHERE
{
  wd:Q8442 wdt:P40/wdt:P40?/wdt:P40? ?descendant.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} 

This will give us the paths of length 1, 2, and 3. ? means ‘zero or one instances’ of the property.
This trick can work with relatively short paths.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading