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)
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.