SPARQL Subgraph Matching: Find Nodes with Specific Property Patterns
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX ex: <http://example.org/> # Find all nodes that have a specific property with a value matching a pattern. # This query is useful for identifying entities based on...
This SPARQL query efficiently identifies nodes within a knowledge graph that possess a specific property whose value conforms to a given regular expression pattern. It's particularly useful for searching and filtering en...
The query selects distinct nodes (`?node`) that have a property `ex:hasName` whose value (`?name`) matches a regular expression. The `FILTER regex(?name, "^[Pp]ro-", "i")` clause is the core of the pattern matching. It checks if the string bound to `?name` starts with 'Pro-' or 'pro-' (case-insensitive due to the 'i' flag). The time complexity is generally linear with respect to the number of triples involving the `ex:hasName` property, as the engine needs to iterate through these and apply the regex. Space complexity is minimal, mainly for storing the matched nodes. The correctness is guaranteed by the precise semantics of SPARQL's `FILTER` and `regex` functions, which perform standard regular expression matching.
SELECT all distinct nodes. FOR EACH node: IF the node has a property 'hasName' AND its value matches the pattern 'starts with "Pro-" (case-insensitive): ADD the node to the result set. RETURN the result set.