Search code examples
arraysregexstringsplitalphabet

Break down a string into an array/list bij alphabet and non-alphabet characters


I need to breakdown a string into an array or list of character sets based on the set beeing alphabet characters and non-alphabet characters.

For example:

string = -"Hello12$th'ere!@4"

the required result = [-"][Hello][12$][th]['][ere][!@4]

Does anyone have an idea of how to get to the required result? I was trying to achieve this by searching for a regex.split solution, but I can't find an expression that suits my needs.


Solution

  • This simple regex does the trick: ([a-zA-Z]+|[^a-zA-Z]+)

    Test it out here.