17

In ANSI SQL, you can write something like:

SELECT * FROM DBTable WHERE Description LIKE 'MEET'

or also:

SELECT * FROM DBTable WHERE Description LIKE '%MEET%'

What I would like help with is writing the SPARQL equivalent of the above please.

Valerio Bozz
  • 1,176
  • 16
  • 32
Kobojunkie
  • 6,375
  • 31
  • 109
  • 164

2 Answers2

24

Use a regex filter. You can find a short tutorial here

Here's what it looks like:

PREFIX ns: <http://example.com/namespace>

SELECT ?x
WHERE
{ ?x ns:SomePredicate ?y .
  FILTER regex(?y, "YOUR_REGEX", "i") }

YOUR_REGEX must be an expression of the XQuery regular expression language

i is an optional flag. It means that the match is case insensitive.

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
  • 2
    Jena has moved to Apache, please, link to the new website here: http://jena.apache.org/tutorials/sparql_filters.html – castagna Jun 17 '12 at 14:59
  • Hi @castagna, your comment is now integrated in the answer. Feel free to remove your comment (I will do it as well if you put a vote on me as ping) – Valerio Bozz Jun 01 '22 at 07:32
5

If you have a fixed string to match you can use that directly in your graph pattern e.g.

PREFIX ns: <http://example.com/namespace>

SELECT ?x
WHERE
{ ?x ns:SomePredicate "YourString" }

Note this does not always work because pattern matching is based on RDF term equality which means "YourString" is not considered the same term as say "YourString"@en so if that may be an issue use the REGEX approach Tom suggests

Also some SPARQL engines provide a full text search extension which allows you to integrate Lucene style queries into your SPARQL queries which may better fit your use case and almost certainly be more efficient for the generic search which would otherwise require a REGEX

RobV
  • 28,022
  • 11
  • 77
  • 119