Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-12-2019 01:13 AM
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.
04-12-2019 01:40 PM
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
04-15-2019 01:01 AM
Yes it would, thanks! (At least until APOC gets an escapemeta() function ).
08-18-2019 06:59 PM
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?
08-19-2019 06:33 AM
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:
[ "[", "]", "(", ")", "?", "+", "*", ".", "\"", "'" ]
08-19-2019 07:00 AM
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!
All the sessions of the conference are now available online