简体   繁体   中英

Regex SPARQL query

Here is my SPARQL query:

  String queryString = 
            "SELECT ?URI "
                    + "WHERE{ ?URI ?predicate ?object ."
                    + "FILTER regex(?URI, \"p\", \"i\")}";


    Query query = QueryFactory.create(queryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, model);
    try
    {
        ResultSet results = qexec.execSelect();
        while(results.hasNext()) {
        QuerySolution soln = results.nextSolution();
        Resource z = (Resource) soln.getResource("URI");
        System.out.println(z.toString());
        }
    finally {
        qexec.close();
    }

As you can see I want to select the URI resources containing letter P. I use regex filter, but II don't get anything in output, not even null pointer exception.

When you are pattern matching a URI, since it is not a literal you need to use str to convert it into string. One way to filter this is just to use contains :

Select ?uri
where {?uri ?p?o.
filter (contains(str(?uri), "p"))
}

or you can use regular expression:

Select ?uri
where {?uri ?p?o.
FILTER regex(str(?uri), "p", "i")}
}

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.

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