0

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.

2 Answers2

2

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

Test it out here.

EvilTak
  • 7,091
  • 27
  • 36
0

You could use Regex.Split with a pattern that looks for a border between a letter and a non-letter usung look-ariund expressions:

var t=Regex.Split(s, @"(?<=[a-zA-Z])(?=[^a-zA-Z])|(?<=[^a-zA-Z])(?=[a-zA-Z])");

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523