Cypher regex query

I am trying to combine nodes in a Neo4j database. Nodes have a name property, and I use a regular expression in Cypher to match this. I only want to combine whole words, so "javascript" should not match if I put the string "java". If the string matches multiple words, that is, a “java script”, I will make two separate requests, one for “java” and one for “script”.

This is what I have so far:

match (n) where n.name =~ '(?i).*\\bMYSTRING\\b.*' return n 

This works, but it does not work with some special characters like "+" or "#". So I can not find "C ++" or "C #", etc. The regular expression in the above code just uses \ b for the word boundary. it also runs away, so it works correctly.

I tried some versions of this post: regex to match a word boundary starting with special characters , but it really didn't work, maybe I did something wrong.

How can I make this work special characters in Cypher and Neo4j?

+5
source share
2 answers

Try to slip away from special characters and look for not dictionary characters, not word boundaries. For instance:

 match (n) where n.name =~ '(?i).*(?:\\W|^)C\\+\\+(?:\\W|$).*' return n 

Although this still has some false positives, for example, the above will correspond to "C +++".

For "Non word character, besides what we want to consider + as a word character", the following may work.

 match (n) where n.name =~ '(?i).*(?:[\\W-[+]]|^)C\\+\\+(?:[\\W-[+]]|$).*' return n 

Although this is not supported by all regexp accessories, and I'm not sure if Neo4j supports this.

+3
source

You can state white spaces (or nothing at all - the border of the match) in front and behind your match, instead of stating the boundaries of words. See this:

 (?i).*(?<!\\S)MYSTRING(?!\\S).* 

Here you can play with a regex demo. It will match your line if it is between spaces or borders for the front and after your word. You can define "punctuation" if you need to, for example:

 (?i).*(?<![^\\s.,$])MYSTRING(?![^\\s.,$]).* ^^^ add boundaries ^^^ 

Then it will also match rawrssss MYSTRING. dd rawrssss MYSTRING. dd

Watch the regex demo!

+1
source

Source: https://habr.com/ru/post/1202860/


All Articles