How to use logical OR in SPARQL regex()?

Jeff picture Jeff · Jul 17, 2010 · Viewed 8.7k times · Source

I'm using this line in a SPARQL query in my python program:

FILTER regex(?name, "%s", "i" )

(where %s is the search text entered by the user)

I want this to match if either ?name or ?featurename contains %s, but I can't seem to find any documentation or tutorial for using regex(). I tried a couple things that seemed reasonable:

FILTER regex((?name | ?featurename), "%s", "i" )
FILTER regex((?name || ?featurename), "%s", "i" )
FILTER regex((?name OR ?featurename), "%s", "i" )
FILTER regex((?name, ?featurename), "%s", "i" )

and each of those without the ()

FILTER regex(?name, "%s", "i" ) || regex(?featurename, "%s", "i" )

What's the right way to do this? Thanks

UPDATE: Using UNION works. But I figured out that it also works if you just repeat the regex() part like so:

FILTER (regex(?name, "%s", "i") || regex(?featurename, "%s", "i" ))

Both solutions seem a little messy in that you have to use a 2-element tuple with copies of the same string to fill in both %ss.

Answer

glenn mcdonald picture glenn mcdonald · Jul 17, 2010

What about this?

SELECT ?thing
WHERE {
  { 
    ?thing x:name ?name .
    FILTER regex(?name, "%s", "i" )
  } UNION {
    ?thing x:featurename ?name .
    FILTER regex(?featurename, "%s", "i" )
  }
}