5
votes

When dealing with properties in Cypher, you can use regular expressions to match property values like so:

Match (n)-[:IS_A]-() where (n:Course_Driving_001) and (n.name =~ '(?i).*criteria.*' or n.description =~ '(?i).*criteria.*')   return distinct n limit 20;

I'd like to do the same thing with a label name. I'd like to get all unique labels that contain a certain string. Something like:

 Match (n)-[:IS_A]-() where (n:Course_*_001) return distinct n;

Can this be done is Cypher? or the RestAPI? Regular expressions?

I'm using Neo4j 2.0 Release.

1

1 Answers

10
votes

You cannot directly use regex on labels. However using the labels function this is possible:

MATCH (n)-[:IS_A]->() 
WHERE any(l IN labels(n) WHERE l=~'Course_*_001')
RETURN distinct n;

Please be aware that this query is potentially expensive as it touches all nodes in your db. You might want to refactor your data model to use multiple labels, e.g. Course and Course_xyz_001. In this case you can use MATCH (n:Course) .... which reduces the number of nodes to visit in first place.