'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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source