Question

The regex is pretty simple:

WHERE t.name =~ "(?i).*\\baaaaaaaa\\b.*"

This works in the Neo4j server web console. I receive the expected results.

When I use the REST interface via Java, I had to change the regex to: "(?i).*\\\baaaaaaaa\\\b.*" (added a back slash). I don't understand why, but it works (again the expected results are returned).

The same regex don't work on embedded Neo4j:

Caused by: Invalid input '': expected '\', ''', '"', 'b', 'f', 'n', 'r', 't' or Unicode (line 1, column 64)
MATCH ... WHERE t.name =~ "(?i)\ aaaaaaaa\ " ...
                                ^

Note that \b isn't shown on the error log (at least on the Intellij console).

To make things worse "(?i).*\\baaaaaaaa\\b.*" is correctly parsed (at least no exception is thrown) both on embedded and server databases. But no results are returned.

Important considerations:

  • Neo4j 2.0
  • I'm using Spring Data (3.0)
Was it helpful?

Solution

Try to use parameters in all 3 cases. Then it should be more consistent.

WHERE t.name =~ {regexp}


params: {regexp: "(?i).*\\baaaaaaaa\\b.*"}

It is the different escaping that happens when you use JSON vs. literal strings in Java, and the Cypher parser so either 2 or 3 different parsers at work each of them having its own understanding of escaping.

In plain Java, afaik it is two backspaces for regexps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top