Gyro
Gyro

Reputation: 823

Regular expression to Replace special characters from node property in neo4j

I have a property whose value may contain following characters: ~!@#$%^&*() and the space character.

I want to replace all of them with an empty string.

Please suggest a suitable regular expression to do this.

Upvotes: 0

Views: 1544

Answers (2)

cybersam
cybersam

Reputation: 67044

There is a thus-far undocumented APOC function, apoc.text.replace, that you can use from your Cypher code. It accepts a regular expression as its second parameter. (Since it is a function, it is not invoked in a CALL clause.)

For example:

RETURN apoc.text.replace('~!@1~!@', '[~!@#$%^&*() ]', '') AS res;

returns:

╒═══╕
│res│
╞═══╡
│1  │
└───┘

Upvotes: 1

Frank Pavageau
Frank Pavageau

Reputation: 11735

You already have the regular expression, it's the class of all the characters you listed:

[~!@#$%^&*() ]

You just have to replace all occurrences by an empty string, using the regex/string API of your language.

For example, in Java:

// The pattern can be declared as a constant, computed only once.
Pattern p = Pattern.compile("[~!@#$%^&*() ]");

String newPropName = p.matcher(propName).replaceAll("");

Upvotes: 1

Related Questions