Cypher 查询正则表达式到 Select 顶点和边

Cypher Query Regex To Select Vertex and Edge

我正在尝试使用匹配条件解析 Cypher 查询以提取其顶点、连接边和 return 条件。它可以有两个以上的正则表达式来查找模式。 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']

对于java脚本版本看post

结尾

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

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

中的正则表达式

抱歉,我对正则表达式字符串使用了 python 风格。在 java.

中使用时请转义所有 \

它应该 return 两组,一组用于 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

对于哪里边

(?:        //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

现在解决方案是相同的,除了两个不同之处,因为 java脚本不允许回溯。差异解释如下:-

/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