How to find a pattern in each element of a list


I have a very beginner question but I didn't find any solution in tutorials and forum.

I would like to search a pattern in a list of strings. Below the '' is only a string and I would like to do the same operation but on a list.

MATCH (n:Person)
WHERE =~ '(?i)AND.*'
RETURN, n.age

I see how to find a exact value in list with (but not with a matching pattern):

MATCH (n:Person)
RETURN, n.age

Thank you very much and sorry if the question was already asked.

Well you can collect that attributes you want in a list like this:

MATCH (n:Person)
with collect(n.names) as names
UNWIND names a n
where =~ <YOUR LOGIC HERE>

This will return you a list a names that you can then UNWIND or loop through and perform your operations on them.

Thank you very much for the help.
However, when I try this to my dataset, i got an error that let me think I cannot use WHERE after UNWIND.

Also these two queries give the same result that I was not expecting


MATCH (n:Person)
with collect(n.names) as names
UNWIND names as n
return n


MATCH (n:Person)
return n.names

Screen of these two examples.
Query without unwind

QUERY with unwind

I was expecting 'collect' to unroll all the element and put it in a simple list whereas this is still a list of list of string.

You can interact with the list after each collect. So you can use WHERE for sure. There are some other APOC methods you can use as well.


Logic for Manipulation in List
MATCH (n:Person)
with collect( distinct n) as names
with [name in names where'B' or'A'] as name
return name

When you have a node with a list property, you can use list predicate functions to test if any, all, or none of the elements of the list meet a predicate.

So in your case, something like this may work:

MATCH (n:Person)
WHERE any(name in WHERE name =~ '(?i)AND.*')
RETURN, n.age

Note that this approach will not leverage indexes. If you do need to leverage indexes like this, you may have to consider restructuring your data model with new kinds of nodes, such as :Alias nodes attached to a :Person, each one with a name (that is indexed), and have your query do an index lookup on the alias name using a lookup that will utilize the index, then MATCH to the :Person nodes for that alias.