Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-06-2021 02:19 PM
Hi all,
I want to remove numbers and special characters from a string by using Cypher. (ie) It must only contain alphabets (a-z). And then also want to sort the characters in that string (asc or desc). It would be a bonus if it's time-efficient. Thanks in advance.
Example: A node contains the property name. Value = 'alan_123walker45@6'.
Output: 'aaaekllnrw' (characters in 'alan walker' sorted in ascending order)
10-07-2021 03:45 PM
APOC Procedures provides some text manipulation functions you'll need to accomplish this.
Here's a query that does this step by step, filtering, splitting, sorting, and joining:
WITH "alan_123walker45@6" as input
WITH apoc.text.regreplace(input, "[^\\p{Alpha}]", "") as filtered
WITH apoc.text.split(filtered, "") as split
WITH apoc.coll.sortText(split) as sorted
RETURN apoc.text.join(sorted, "") as final
Here's doing all of this at once:
WITH "alan_123walker45@6" as input
RETURN apoc.text.join(apoc.coll.sortText(apoc.text.split(apoc.text.regreplace(input, "[^\\p{Alpha}]", ""), "")), "") as final
Note that for regreplace(), we're using the patterns present in java.util.regex.Pattern.java, taking account for escaping special characters:
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
If you want conversion to lowercase, you would want to add a toLower() function call in there too, otherwise you'll be working with upper and lowercase characters.
This should be fairly efficient, basic string operations should be quick.
All the sessions of the conference are now available online