# 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- 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

2 Likes

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.

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?