Created
August 22, 2017 17:32
-
-
Save jindrichmynarz/630226f2f53af92332843f0747a3ab85 to your computer and use it in GitHub Desktop.
Split strings in pure SPARQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one. Just one remark here for anyone using this in a
SELECT
. UseDISTINCT
, 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 :)