Escaping the inescapable

Posting this just as a help for others, as i did not find any article directly solving this issue: escaping stuff like dot, comma, slash, start, question mark, parenthesis, plus, single quote or double quote [ "[", "]", "(", ")", "?", "+", "*", ".", """, "'" ]

I mean, it's enough that the Cypher query builder is not that precise, APOC input parameters are too generic and examples need some rework, and also checking the closure of parenthesizes would need some improvement, but, when you need to add your query with already escaped characters as an APOC embedded sub-query, it's just frustrating.

So, for example, if you initially had to escape something in your initial query, like : apoc.text.split(map['Tree Path'],'\\.') , and used \ to escape . only to find out it that this also needs escaping because the parameter is a REGEX (thank you for the REGEX but please do one with simple string) and ' for the attribute name, then, in your embedded sub-query you will need to: escape ' as \\' in your initial query (see otherwise) and escape the \ as \\\\ (4 slashes, yes) in the sub-query, so that in the end it looks like apoc.text.split(map[\'Tree Path\'],\'\\\\.\'))

Otherwise:```Invalid input '.': expected '', ''', '"', 'b', 'f', 'n', 'r', 't', UTF16 or UTF32 (line 11, column 118 (offset: 425))
"case map['Tree Path'] when '(Root Code)' then -1 when map['Id'] then -2 else size(apoc.text.split(map['Tree Path'],'.')) end"

Disclaimer: posting images seems not to work,only the link is displayed


It also depends on if you're using the neo4j-browser vs passing the string in via one of the many drivers.

I've found that the browser, likely due to HTML, needs an extra level of escaping the \. Resulting in the 4x \ requirement. Notice, you can't type a slash in here and have it show up. \\ <= that is me typing 4 slashes. If you wrap it in the ticks for code, it shows up as you want.

I think your battle is with HTML escape syntax, on top of java escape syntax.

Hope that's helpful.


1 Like

One trick I use to escape . is use


It's also cleaner looking that multiple backslashes!

1 Like