Cypher查询正则表达式选择顶点和边缘

时间:2017-09-19 03:41:33

标签: javascript regex cypher lex

我试图用匹配条件解析Cypher查询以提取其顶点,连接边缘和返回条件。它可以有两个正则表达式来查找模式。可能是Regex1和Regex2:

MATCH (A)-[E1]->(B), (A)-[E2]->(C), (B)-[E3]->(C)
WHERE E1.TYPE = ''marriedTo'' AND C.NAME = ''Ares''
RETURN A.NAME AS PARENT1_NAME, B.NAME AS PARENT2_NAME

Regex1输出= ['(A)-[E1]->(B)','(A)-[E2]->(C)','(B)-[E3]->(C)']

Regex2输出= [E1.TYPE = "marriedTo" , 'C.NAME = "Ares"]

MATCH (a)
RETURN a.NAME AS name
ORDER BY a.NAME ASC

Regex1输出= ['(a)']

Regex2输出= []

MATCH (a), (A)
RETURN a.NAME AS name

Regex1输出= ['(a)','(A)']

Regex2输出= []

MATCH (a)-[e]-(b)
RETURN e.TYPE AS type
ORDER BY e.TYPE ASC

Regex1输出= ['(a)-[e]-(b)']

Regex2输出= []

MATCH (A)-[E1]->(B), (B)-[E2]->(C)
WHERE A.NAME = 'Zeus'
RETURN A.NAME as ANAME, B.NAME AS BNAME, C.NAME AS CNAME
ORDER BY B.NAME ASC, C.NAME ASC

Regex1输出= ['(A)-[E1]->(B)','(B)-[E2]->(C)']

Regex2输出= [A.NAME = 'Zeus']

1 个答案:

答案 0 :(得分:3)

对于javascript版本,请查看帖子的结尾

(?<=MATCH)(?:(?:.*?=)?\s+(.+?)\s+)(?=WHERE|RETURN)(?:WHERE\s+(.+?)\s+(?=RETURN))?

查看https://regex101.com/r/aWo08j/1/

工作的正则表达式

抱歉,我使用了python flavor作为正则表达式字符串。在java中使用时,请转义所有\

它应该为MATCH子句返回两个组,为WHERE子句返回第二组。因此,您可以使用一个正则表达式获得这两个部分。

下面的说明; -

符合条件

(?<=MATCH)   //positive look behind match only that appears after this
(?:          //Non capturing group START match this but do not capture
(?:.*?=)     //Do not capture till first equal to sign in match.
\s+          //Match any spaces
(.+?)         //Match and CAPTURE our match condition
\s+         //Match any spaces
)           //Non capturing group END
(?=WHERE|RETURN) //Positive lookahead match only if followed by WHERE or RETURN

For Edge Edges

(?:        //Non capturing group START
WHERE      //Match WHERE but do not capture
\s+        //Match space after WHERE but do not capture
(.+?)       //This is our where clause MATCH and CAPTURE!
\s+        //Match space after where clause but do not capture
(?=RETURN) //Positive Lookahead capture only if followed by RETURN
)          //Non capturing group END
?          //The whole WHERE clause may or may not occur.

正在运行的Java版本https://repl.it/LR5s/11

在此处修改了OP请求和添加JS版本的注册

Java脚本版本

/(?:^MATCH(?:.*?=)?\s+(.+?)\s+)(?=WHERE|RETURN)(?:WHERE\s+(.+?)\s+(?=RETURN))?/img

现在解决方案是相同的,除了两个不同之处,因为javascript不允许隐藏。差异解释如下: -

/regex-expression/options //This is the format for javascript regular expression
^                         //Beginning of a line
MATCH                     //MATCH is now brought inside the non capturing group as there is no lookbehind in js
/img                      //options i-case insensitive; m-multiline; g-global don't stop after first match

正则表达式的其余部分保持不变。就像之前我们有两个小组会回馈我们的match顶点和where边缘。

此示例js为https://repl.it/LTis/1