Escaping regex metacharacters in Cypher

Is there a way to escape regex metacharacters (. + * ? etc) in a Cypher query? I want to do something like this:

MATCH (n), (m)
WHERE =~ '.*\b' + + '\b.*'

However there will be a problem if happens to contain metacharacters, so I would like to call a function/procedure which would escape them before doing the regex operation, something like the quotemeta() function in Perl.

Ok, its kind of ugly, but would this work?

WITH "Here is (my) * test string? with some + meta." as mystring
WITH *,["[","]","(" ,")", "?" ,"+" , "*" ,"."] as regexmetachars
WITH *,reduce(v = mystring, char in regexmetachars | apoc.text.replace(v, '[\\'+char+']{1}', '\\\\'+char)) as filteredstring
RETURN mystring,filteredstring
1 Like

Yes it would, thanks! (At least until APOC gets an escapemeta() function :slightly_smiling_face:).

How would one want to do this same approach if the character in question is ". I am streaming in some raw data with apoc.periodic.iterate and apoc.load.csv and I know the data can have some misc ' and " in there. Any thoughts?

You could pre-process the CSV file to escape the quotes before the import operation, as described here: How do I use LOAD CSV with data including quotes - Knowledge Base

Alternatively, you could add " and ' to the list of metacharacters in Paul's solution, i.e. the list would become:

[ "[", "]", "(", ")", "?", "+", "*", ".", "\"", "'" ]

I did think of the second recommendation last night after sending my message. Still ran into an issue, but I am working to figure that out. Will post once I figure out my issue.

This is a good post!