1

For Example: "john@mora.org" is the email address and, I want to extract mora from this email using SPARQL

This is my approach which didn't give the expected result:

BASE <http://timbl.org/foaf.rdf>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?work_place
FROM <http://timbl.org/foaf.rdf>
WHERE
{
<#timbl> foaf:knows ?friend .
?friend foaf:name ?name .
?friend foaf:mbox ?mail .
BIND(REPLACE(STR(?mail), "^.*@", "", "i") as ?work_place) .
}

Example input:

"john@mora.org"

Expected Result:

"mora"

The result I Received :

"mora.org"

What needed to be added in the query or RegEx to get the expected output

Niroshan Ratnayake
  • 3,433
  • 3
  • 20
  • 18
  • given that SPARQL does support `regex`, just use it. – UninformedUser May 26 '21 at 07:31
  • @UninformedUser yes that is what I want how that can be retrieved using regex in sparql? – Niroshan Ratnayake May 26 '21 at 07:36
  • 1
    the pattern to use is `BIND (REPLACE("THE_STRING_HERE", "THE_REGEX_HERE", "THE_REGEX_GROUP_HERE") AS ?str)` in your SPARQL query – UninformedUser May 26 '21 at 07:50
  • @UninformedUser what should I be adding to "The_REGEX_HERE" and "THE_REGEX_GROUP_HERE"? – Niroshan Ratnayake May 26 '21 at 07:54
  • BIND(REPLACE(?email_address, "[^@]*$") AS ?work_place) . whats wrong with this? – Niroshan Ratnayake May 26 '21 at 07:54
  • it takes 3 arguments if you look carefully at my comment. I mean, you want to extract something, right? The 3rd argument should be the regex group that you want to use to replace the whole string – UninformedUser May 26 '21 at 08:35
  • 1
    The function `REPLACE( string, pattern, replacement, flag )` returns the string after replacing all occurrences of `pattern` in `string` with `replacement`. The `replacement` can contain `$n` or `${name}`, which are replaced by the corresponding numbered or named capture group in the pattern. An optional flag affects the regular expression pattern, just as with the flag argument to REGEX(). – UninformedUser May 26 '21 at 08:36
  • @UninformedUser BIND(REPLACE(?email_address, "[^@]*$") AS ?work_place) .why this wont work? – Niroshan Ratnayake May 26 '21 at 08:43
  • 1
    because it takes **3 argument**, not just two. The signature is `REPLACE( string, pattern, replacement, flag )` with `flag` being optional. One more time, this method does replace in the given `string` all matches of the `pattern` by the given `replacement` - this is pretty trivial. If you do `REPLACE( "abab", "a", "d")` it returns `"dbdb"` – UninformedUser May 26 '21 at 08:46
  • Does this answer your question? https://stackoverflow.com/questions/40180792/parsing-sparql-results-to-obtain-hostname – tink May 29 '21 at 00:45
  • @tink no that didn't help. thank you – Niroshan Ratnayake May 29 '21 at 12:11

1 Answers1

0

Your approach only deleted the part of the string you matched, if you also wanna match and delete another substring you can just nest your replaces. Something like this should work.

 BASE    <http://timbl.org/foaf.rdf>
 PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
 
 SELECT  ?name ?work_place
 FROM <>
 WHERE
   { <#timbl>  foaf:knows  ?friend .
     ?friend   foaf:name   ?name ;
               foaf:mbox   ?mail
     BIND(replace(replace(str(?mail), "^.*@", "", "i"), "\..*$", "", "i") AS ?work_place)
   }
NechesStich
  • 196
  • 4