在Cypher查询中使用正则表达式捕获组 [英] Using regex capture groups in a Cypher query
问题描述
在密码文档中,它说您可以使用正则表达式匹配字符串属性中特定的(以前已知的)模式.
是否也可以使用捕获组?
举个例子:
我的节点属性是序列化的JSON,如下所示:
n.json = '{"name": "John", "gender": "m"}'
n.json = '{"name": "Jane", "gender": "f"}'
我想知道不同性别的人之间的关系:
MATCH r=(n)--(m)
WHERE NOT (n.json =~ '.*gender": "(\c)".*')[1] = (m.json =~ '.*gender": "(\c)".*')[1]
RETURN r
或类似的东西.
此处是一个相关的问题.简短的答案是cypher不支持反向引用.正则表达式仅用于匹配.
通常,当我遇到您所面临的问题时,我会尝试在导入步骤之前进行处理. IE.您可以从数据开始,将其处理为CSV,然后将CSV加载到图形中.在处理CSV的过程中,我将与其他一些工具(sed/awk/perl/python/其他)进行模式匹配,然后在将数据加载到图形中之前先对其进行修改.>
In the cypher documentation it says you can use regexes to match particular -previously known- patterns in a string attribute.
Is it possible to use capture groups as well?
Take an example:
My node attributes are serialised JSON that look like:
n.json = '{"name": "John", "gender": "m"}'
n.json = '{"name": "Jane", "gender": "f"}'
I want to know relationships between people of different gender:
MATCH r=(n)--(m)
WHERE NOT (n.json =~ '.*gender": "(\c)".*')[1] = (m.json =~ '.*gender": "(\c)".*')[1]
RETURN r
or something like that.
Here is a related question. The short answer is that backreferences aren't supported in cypher; regular expressions are just for matching.
In general, when I run into problems like what you're facing, I try to deal with them prior to the import step. I.e. you might start with data, massage it into CSV, and then load the CSV into a graph. During the manipulation of the CSV, I'd do the pattern matching with some other tool (sed/awk/perl/python/whatever) and then modify the data before it gets loaded into the graph to do this sort of thing.
这篇关于在Cypher查询中使用正则表达式捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!