ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

SPARQL Subgraph Matching: Find Nodes with Specific Property Patterns

SPARQL

Goal -- WPM

Ready
Exercise Algorithm Area
1PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
2PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
3PREFIX ex: <http://example.org/>
4
5# Find all nodes that have a specific property with a value matching a pattern.
6# This query is useful for identifying entities based on partial string matches.
7# For example, finding all products whose names start with 'Pro-'.
8
9SELECT DISTINCT ?node
10WHERE {
11# ?node is the variable representing the node we are looking for.
12?node ex:hasName ?name .
13# We are interested in nodes that have a property 'ex:hasName'.
14# The value of this property is bound to the variable ?name.
15
16# Use FILTER and REGEX to match the ?name against a pattern.
17# The pattern here is a regular expression.
18# '^[Pp]ro-' matches strings that start with 'Pro-' or 'pro-'.
19FILTER regex(?name, "^[Pp]ro-", "i")
20# The 'i' flag makes the match case-insensitive.
21}
Algorithm description viewbox

SPARQL Subgraph Matching: Find Nodes with Specific Property Patterns

Algorithm description:

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 entities based on partial string matches, such as finding all resources whose names start with a certain prefix or contain a specific substring. This is a fundamental operation for data exploration and targeted retrieval in semantic web applications.

Algorithm explanation:

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.

Pseudocode:

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.