Proper Cypher regular expression examples

I have been for weeks reading and trying to figure out how to use regular expressions in Cypher. And no the website doc has next to nothing there, also that site everyone send me to doesn't actually show neo4j cypher way of doing it.

So my question is. What is the exact code to write the equivalent in cypher of this: \s[a-zA-Z]

Can someone write some normal regular expressions with then the cypher version next to it, because the documentation has nothing that would give you any idea how to write meaningful expressions.

Kind regards,

Hello @tideon :slight_smile:

In the documentation, there is a little example you can transform:

MATCH (n:Person)
WHERE =~ '\s[a-zA-Z]'
RETURN, n.age

But I think you already tried that so:

  • can you tell us what you are trying to achieve with the regex?
  • can you tell us what the query should do?
  • can you give us your model?


Hello Cobra,

I think that the regex isn't working because I copy pasted the where statement above and got the following error.

Here is the query
match (t:Toy)
WHERE t.ModelNr=~ '\s[a-zA-Z]'
return t

Invalid input 's': expected '\', ''', '"', 'b', 'f', 'n', 'r', 't', UTF16 or UTF32 (line 2, column 21 (offset: 34))
"WHERE t.ModelNr=~ '\s[a-zA-Z]'"

I am attemting to find the toys where the model nr has letters in the field, so I can identify the ones that need to be cleaned up.

So lets start with this and go from there. If what you gave me doesn't work then something is fundamentally wrong.

Are you from Neo4J?

The internet is filled with people who can't get regular expression to work in Neo4j. I spend over six hours yesterday reading every post I can find on the internet, So many people cannot get a clear understanding form the manual, as it give very little explanation.

I think there is a lot of confusion of how regEx works in Neo4j

Here is a good example that highlights what i have read on multiple forums including here and stack Overflow

That very short excerpt in the manual doesn't go into what fags there are, what each one mean, how to write a query that gets back digits.

watched and learned this very good tutorial on regular expressions, but can't translate that knowledge into cypher

Many / all things are not working.

Here is another example

people refer to java, but there no robust examples of actual uses so you can see how it is actually written. Even the O'reilly "Graph databases", don't cover the topic. It is a very powerful option to have, but there is next to no attention given to it.

Yeah it's weird, I just know regex from Neo4j are from Java. No, I'm not working for Neo4j :slight_smile:

I do not see the syntax error to be honest :confused:

Hello Cobra,

I figured it out.

All the the metacharacters need to be escaped. So "\s" needs to be "\s"

The manual doesn't address this at all.

So now I understand how to use regular expressions. Is there way to extract a part of a string that matches a criteria and put into another field?

So for instance: Technic 42020: Twin-Rotor Helicopter
That I would extract "42020" and put in a propery ModelNr

SET t.ModelNr = 42020

You should find what you need here:

I read everything there, but nothing gave the impression that I can use to achieve my goal.

Do you have an example of how it could be achieved?

I now know how to fully use Regex in Cypher, but no way to extract the pattern I have found to put it in a property field.

Thanks in advance,

You need to use the APOC regex functions.

This is what you want:

I'm reading it documentation, this is exactly what I needed.

Hello Clem,

So I have reach so far:

CALL apoc.load.json("file:///lego3-5.json") YIELD value 
WITH value AS v
WITH apoc.text.regexGroups ( v.Product_Price, '\\d{1,3},\\d{1,2}' ) as price

RETURN price


And I get an output of ( See attachment), but I cant use unwind to get them out of the nested list. Was at it for about 6 hours getting to this point and trying everything I can. The manual didn't explain how to use it further than the Return example.

I keep asking myself, why is the manual so bad. I never really explains as you would expect a manual to explain a subject, it is often a brief overview.

Hello @tideon :slight_smile:

Could you provide us the JSON file?


[edit because I misread the screen shot....]

Try returning price[0] to get the first element of the list of price(s) instead of the list of the single price. (European style of numbers with comma instead of decimal point.)

Hey Cobra,

Here is a sample of the data.
had to give it the .txt extension so I could upload it.
sample_lego.json.txt (389 Bytes)

Hello Clem,

I uploaded a file with a sample of the data.
As you will see the price has a comma in the price ( europe ). So the RegEx is written to capture that. It is one price.

oops. My eyes are so good, so I misread it.

price[0] will give you a string. But if you want to store it as a float, you'll need to substitute the "," for a "." and then do toFloat()

the issue still remains, as to not being able to get the value out of this nested list, and I don't understand why apoc is doing that, the manual is very limited.

It is one price. so for instance 123,50
In europe we use comma's and I also want it to keep the comma, because all prices are written that way.

I have tweaked it in everyway I can so see if something would work.