Skip to content

Instantly share code, notes, and snippets.

@jindrichmynarz
Created August 22, 2017 17:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jindrichmynarz/630226f2f53af92332843f0747a3ab85 to your computer and use it in GitHub Desktop.
Save jindrichmynarz/630226f2f53af92332843f0747a3ab85 to your computer and use it in GitHub Desktop.
Split strings in pure SPARQL
# Solution from: <http://answers.semanticweb.com/questions/19162/split-and-trim-strings-in-sparql/27544>
PREFIX schema: <http://schema.org/>
DELETE {
?s schema:url ?urls .
}
INSERT {
?s schema:url ?url .
}
WHERE {
VALUES ?n { 1 2 3 4 5 6 7 8 9 10 } # Split at most 10 URLs mangled together
?s schema:url ?urls .
FILTER contains(?urls, "; ") # Only split multiple URLs
BIND (concat("^([^,]*,){", str(?n) ,"} *") AS ?skipN)
BIND (replace(replace(?urls, ?skipN, ""), ",.*$", "") AS ?url)
}
@tpluscode
Copy link

Nice one. Just one remark here for anyone using this in a SELECT. Use DISTINCT, else you will get the last value multiple times as to always return ?n results.

Might also tweak the regex pattern but DISTINCT is simpler and in some cases may not be needed at all anyway :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment