cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Some troubles with exercises

  1. Testing with regular expressions

MATCH (p:Person)
WHERE p.name =~’Tom.*’
RETURN p.name

What does 'regular expressions' mean? And I've never seen the .* pattern before, I thought the dot operator is always used to retrieve a property of something?
/
2) Why doesn't this query work?

MATCH (p)-[r]-(m)
WHERE p:Person AND type(r)='WROTE' AND type(m)="Movie"
Return p.name, m.title

is type() only applicable to relationships and not labels?
/
3) When to use CONTAIN vs IN?

MATCH (a:Person)-[r:ACTED_IN]->(m:Movie)
WHERE m.title IN r.roles
RETURN m.title as Movie, a.name as Actor

Vs

MATCH (a:Person)-[r:ACTED_IN]->(m:Movie)
WHERE m.title CONTAINS r.roles
RETURN m.title as Movie, a.name as Actor

/

1 ACCEPTED SOLUTION

1- The characters inside the ' ' are part of the regular expression, not the Cypher language anymore. A regular expression is something that describe a pattern instead of a specific sequence of characters. Ex: 123 is a sequence of 3 numbers. 'sequence of tree numbers' is the pattern describing it. In the regex language you can write it as /d{1,3}

The . symbol in a regular expression means roughly any characters.
The * symbol in a regular expression means repeat the preceding character 0 to infinite times.

So Tom.* means every name who begins by the exact characters Tom whatever the characters after the Tom word.

2 - type function is used to know the type of a relation only, a node () doesn't have a type but a set of labels you can use the labels() function for them.

3- IN check if a element is a part of a list
CONTAINS check if a string contains a substring

Checkout the FREE Graph Academy introduction course

View solution in original post

6 REPLIES 6

1- The characters inside the ' ' are part of the regular expression, not the Cypher language anymore. A regular expression is something that describe a pattern instead of a specific sequence of characters. Ex: 123 is a sequence of 3 numbers. 'sequence of tree numbers' is the pattern describing it. In the regex language you can write it as /d{1,3}

The . symbol in a regular expression means roughly any characters.
The * symbol in a regular expression means repeat the preceding character 0 to infinite times.

So Tom.* means every name who begins by the exact characters Tom whatever the characters after the Tom word.

2 - type function is used to know the type of a relation only, a node () doesn't have a type but a set of labels you can use the labels() function for them.

3- IN check if a element is a part of a list
CONTAINS check if a string contains a substring

Checkout the FREE Graph Academy introduction course

A perfect explanation, thanks!

Correction:

CONTAINS check if a string contains a substring*
IN check check if a element is a part of a list

I corrected it in the original post too

Hey Gabriel, I come across another instance of IN that got me confused... take this excerise:

Retrieve the movies that have an actor’s role that is the name of the movie, return the movie title and the actor’s name.

This is the answer given:

MATCH (a:Person)-[r:ACTED_IN]->(m:Movie)

WHERE m.title in r.roles

RETURN m.title as Movie, a.name as Actor

I thought it would be CONTAINS since m.title should be a string.
Is it because r.roles returns a list? A list of strings?

Short answer : Yes

Long answer:

IN check if a element is a part of a list
where element can be any kind of primitive data type including a string

In this case, the property roles of the ACTED_IN relationship referred by the r variable is a list of strings, and m.title is a simple string. So m.title IN r.roles means in english :

Is the value of the property m.title ( a string ) a part of the r.roles property's value ( A list )
So is the string an element of this list of string

Gotcha. Thanks again!