Escaping string for using in cypher regex

1,351 views
Skip to first unread message

Jean-Pierre Bergamin

unread,
Apr 3, 2012, 7:16:19 AM4/3/12
to ne...@googlegroups.com
Hello neo4j users

Is there some easy way to escape a string for using it as a literal in a cypher regex in Java?
It would be a nice feature if cypher regexes would support the \Q...\E escape sequence that is found in java.util.regex, so one could use the Pattern.quote(...) method to do the escaping.



Best regards,
James

Michael Hunger

unread,
Apr 3, 2012, 7:21:55 AM4/3/12
to ne...@googlegroups.com
Why not using a parameter for the actual regexp, then you can pass in a default java regexp?

Michael

Jean-Pierre Bergamin

unread,
Apr 3, 2012, 8:25:26 AM4/3/12
to ne...@googlegroups.com
Am 3. April 2012 13:21 schrieb Michael Hunger
<michael...@neotechnology.com>:

> Why not using a parameter for the actual regexp, then you can pass in a default java regexp?

How would that look like?

Hm. After further tests, it seems as it is not possible to escape
anything in a regex at all.
E.g. "....WHERE d.name =~/.*a\\.c.*/...."results in a parsing error:

illegal start of value
"START ref=node(0) MATCH ref-[:TEST]->d WHERE d.name =~/.*a\.c.*/ RETURN d"
^
at org.neo4j.cypher.internal.parser.v1_6.CypherParserImpl.parse(CypherParserImpl.scala:65)
at org.neo4j.cypher.CypherParser.parse(CypherParser.scala:42)

This seems like a missing piece: escaping in regular expressions.

Full test code:
@Test
public void regexTest() {
GraphDatabaseService db = neo4j.getGraphDatabaseService();

Node d = db.createNode();
d.setProperty("name", "foo a.c bar");
Node e = db.createNode();
e.setProperty("name", "foo aXc bar");
db.getReferenceNode().createRelationshipTo(d,
DynamicRelationshipType.withName("TEST"));
db.getReferenceNode().createRelationshipTo(e,
DynamicRelationshipType.withName("TEST"));
String query = "START ref=node(0) MATCH ref-[:TEST]->d WHERE
d.name =~/.*a\\.c.*/ RETURN d";

ExecutionEngine engine = new ExecutionEngine(db);
ExecutionResult result = engine.execute(query);
Iterator<Node> n_column = result.columnAs("d");
while (n_column.hasNext()) {
System.out.println(n_column.next().getProperty("name"));
}
}


Best regards,
James

Michael Hunger

unread,
Apr 3, 2012, 8:32:48 AM4/3/12
to ne...@googlegroups.com
It should look like this:

query("START ref=node(0) MATCH ref-[:TEST]->d WHERE d.name =~ {param} RETURN d", map("param",".*a\\.c.*"))

Michael

Jean-Pierre Bergamin

unread,
Apr 3, 2012, 10:36:46 AM4/3/12
to ne...@googlegroups.com
That works, thanks.

What does the actual escaped string look like? Any chance to get the
string representation of the query from the ExecutionEngine somehow?


Best regards,
James

Am 3. April 2012 14:32 schrieb Michael Hunger
<michael...@neotechnology.com>:

Andres Taylor

unread,
Apr 3, 2012, 10:47:26 AM4/3/12
to ne...@googlegroups.com
On Tue, Apr 3, 2012 at 4:36 PM, Jean-Pierre Bergamin <jpber...@gmail.com> wrote:
That works, thanks.

What does the actual escaped string look like? Any chance to get the
string representation of the query from the ExecutionEngine somehow?

Cypher doesn't have escaped strings right now. That's the real problem...

Andrés 
Reply all
Reply to author
Forward
0 new messages