Regular expression on the string of a property name in Cypher

340 Views Asked by At

I know it is possible to use regular expressions for property values like for example:

MATCH (n)
WHERE n.SomeProperty =~ 'somestring*'
RETURN n;

What i want is to use regular expression on the property name and check for all the properties which start with a certain string like for example:

MATCH (n)
WHERE n.`SomeProperty*` > 10
RETURN n;

So I want to have all nodes which have a property which begins with 'SomeProperty' and have a value > 10 for this property.

This doesn't seems possible with using regular expressions like in my example. I've tried it and with my research i couldn't find a solution. Does anyone have an idea how to achieve this using another technique ?

2

There are 2 best solutions below

1
On BEST ANSWER

Given the following test graph

CREATE (:TestNode {somePropertyOne: 10})
CREATE (:TestNode {somePropertyTwo: 11})
CREATE (:TestNode {somePropertyThree: 12})
CREATE (:TestNode {someOtherProperty: 13})

The following query achieves what you want

MATCH (n)
WHERE ANY(x IN keys(n) WHERE x STARTS WITH 'someProperty' AND n[x] > 10)
RETURN n

╒════════════════════════╕
│"n"                     │
╞════════════════════════╡
│{"somePropertyTwo":11}  │
├────────────────────────┤
│{"somePropertyThree":12}│
└────────────────────────┘

Bear in mind that its really not an optimized query for graphs, so it will be slow on decent size databases.

0
On

I created sample nodes as below:

Create (n1:RexNode {someproperty10: 10}),
   (n2:RexNode { someproperty11: 11}),
  (n3:RexNode {someproperty12: 12})

Then I used this query to return n2 and n3. As you can see n1 starts with someproperty but the value is not greater than 10. The quantifier is ANY so it will only look for at least one property (key of node n) and it will return it.

MATCH (n)  
WITH n  
   WHERE 
     ANY( k in  keys(n) 
          WHERE k STARTS WITH 'someproperty' 
            AND n[k] > 10
        )
RETURN n

Result:

╒═════════════════════╕
│"n"                  │
╞═════════════════════╡
│{"someproperty11":11}│
├─────────────────────┤
│{"someproperty12":12}│
└─────────────────────┘