cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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 n.name =~ '.*\b' + m.name + '\b.*'

However there will be a problem if m.name 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.

5 REPLIES 5

pdrangeid
Graph Voyager

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

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

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: https://neo4j.com/developer/kb/how-do-i-use-load-csv-with-data-including-quotes/

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!