Using regular expressions beyond matching in Cypher

北城余情 提交于 2019-12-09 03:46:34

问题


I make the following query

neo4j-sh (?)$ start n=node(*) where n.name  =~ 'u(.*)' return n; 
==> +-----------------------+
==> | n                     |
==> +-----------------------+
==> | Node[311]{name:"u1"}  |
==> | Node[312]{name:"u2"}  |
==> | Node[313]{name:"u3"}  |
==> | Node[314]{name:"u4"}  | 

I want to add a "userId" property and set it the number in the name key. I mean I want

==> +-----------------------+
==> | n                     |
==> +-----------------------+
==> | Node[311]{name:"u1", userId:'1'}  |
==> | Node[312]{name:"u2", userId:'2'}  |
==> | Node[313]{name:"u3"},userId:'3'  |
==> | Node[314]{name:"u4"}, userId:'4' | 

Now I need to strip the numbers from n.name. How can I do this? How can I get the value from the (.*) in regex?


回答1:


You can't do that in Cypher (as far as I know)--regex is just for matching.

If it's always just a single letter in front of it, you can take the substring:

start n=node(*) 
set n.userId = substring(n.name, 1)



回答2:


I had the same issue, so I developed a tiny Neo4j server plugin that lets you run regular expressions over REST API and against string node properties for matching/splitting/substituting purposes. It can return the results inside Neo4j console or save them to a property.

Have a look at it, maybe you find it useful: https://github.com/mszargar/Regx4Neo

It takes just a minute to install, but you will have to restart Neo4j for it to take effect.



来源:https://stackoverflow.com/questions/17351069/using-regular-expressions-beyond-matching-in-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!