Renaud
Renaud

Reputation: 4680

Using regex capture groups in a Cypher query

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.

Upvotes: 2

Views: 1380

Answers (2)

62mkv
62mkv

Reputation: 1542

I was able to use matched groups with an APOC apoc.text.regexGroups function:

MATCH (a:AppJar)-[:CONTAINS]->(b)
WHERE b.fileName STARTS WITH "/BOOT-INF/lib/event-"
WITH a, b, last(last(apoc.text.regexGroups(b.fileName, '/BOOT-INF/lib/event-([\\d\\.]+).jar'))) as version
SET b.version = version
RETURN a.appName, b.fileName, b.version
LIMIT 100

Upvotes: 0

FrobberOfBits
FrobberOfBits

Reputation: 18022

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.

Upvotes: 4

Related Questions