I am trying to extract a part of a string by using index numbers. When having
"OEIUFHWOIEFWNFOPQWJFHQWOIHFEB..."
and I need string from 5:10 (FHWOIE)
I found out that it is not possible via REGEX as that returns only boolean and not groups. However, I did not manage to find a region selection on strings via positions. Now I am wondering if there is any?
I found out that it is partly possible via...
BIND(REPLACE(?sequence, '^.{100}', "") AS ?sequencestrip1)
but not
BIND(REPLACE(?sequence, '^.{?start}', "") AS ?sequencestrip1)
I think this does it for anyone who is interested:
BIND(REPLACE(?sequence, "^.{"+str(?start)+"}", "") AS ?sequencestrip1)
and of course to remove the area behind what you are interested in
BIND(REPLACE(?region, ".{"+str(strlen(?region)-10)+"}$", "") AS ?upstream)
In the first SPARQL Query Language for RDF , this would be rather difficult, because there are not many string manipulation functions. However, in your question, you've used replace
which appeared in SPARQL 1.1 Query Language . This is good for you because, in addition to replace
, SPARQL 1.1 includes more string manipulation functions. One of these, substr
, does exactly what you need. For instance, here's a query in which ?string
is bound to the string you mentioned, and substr
is used to extract the substring you're looking for and bind it as ?substring
.
select * where {
values ?string { "OEIUFHWOIEFWNFOPQWJFHQWOIHFEB..." }
bind( substr( ?string, 5, 6 ) as ?substring )
}
The results are:
--------------------------------------------------
| string | substring |
==================================================
| "OEIUFHWOIEFWNFOPQWJFHQWOIHFEB..." | "FHWOIE" |
--------------------------------------------------
Note that the second argument to substr
is the starting position (where the first index is 1), and the third is the length of the substring, not the final position. You wanted a substring, FHWOIE
, that has six characters, to the third argument is 6.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.