简体   繁体   English

SPARQL字符串RANGE

[英]SPARQL string RANGE

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. 我需要从5:10 (FHWOIE)字符串我发现它不可能通过REGEX,因为它只返回布尔而不是组。 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. RDF的第一个SPARQL查询语言中 ,这将是相当困难的,因为没有很多字符串操作函数。 However, in your question, you've used replace which appeared in SPARQL 1.1 Query Language . 但是,在您的问题中,您使用了SPARQL 1.1查询语言中出现的replace This is good for you because, in addition to replace , SPARQL 1.1 includes more string manipulation functions. 这对你有好处,因为除了replace ,SPARQL 1.1还包含更多的字符串操作函数。 One of these, substr , does exactly what you need. 其中之一, substr ,完全符合您的需求。 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 . 例如,这是一个查询,其中?string绑定到您提到的字符串, substr用于提取您正在寻找的子字符串并将其绑定为?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. 请注意, substr的第二个参数是起始位置(第一个索引为1),第三个参数是子字符串的长度 ,而不是最终位置。 You wanted a substring, FHWOIE , that has six characters, to the third argument is 6. 你想要一个子字符串FHWOIE ,它有六个字符,第三个参数是6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM