c# - RegEx split/tokenize string when character within the string changes -



c# - RegEx split/tokenize string when character within the string changes -

racking brain on one. have string e.g. mon-123abc/456 78#abcd

what want array or list follows

[0] = mon [1] = - [2] = 123 [3] = abc [4] = / [5] = 456 [6] = ' ' (space character between 6 , 7 in illustration string) [7] = 78 [8] = # [9] = [10] = b [11] = c [12] = d

i want split string input when there transition 1 type of character (alpha, numeric, non-alpha/numeric, upper case lower case) another

either regexp or c# code do. i've got simple regex 0+|(?<=([1-9]))(?=[1-9])(?!\1) splits on numeric, regex not good. i've played c# code loop thro' string have problem transition between character types.

example 2: illustration input string maybe 123qaz zbc/45678#ab-cd

it's spiting on each transition not position that's key. in illustration 2 there 2 spaces between z , z way. mentioned it's transition between types that's key.

here's solution doesn't utilize regular expressions.

public static ienumerable<string> splitontype(string str) { stringbuilder builder = new stringbuilder(); int previoustype = -1; foreach (char c in str) { int type; if ('a' <= c && c <= 'z') type = 0; else if ('a' <= c && c <= 'z') type = 1; else if ('0' <= c && c <= '9') type = 2; else type = 3; if (previoustype != -1 && type != previoustype) { yield homecoming builder.tostring(); builder.clear(); } builder.append(c); previoustype = type; } if (builder.length > 0) yield homecoming builder.tostring(); }

note grouping non-alphanumeric characters per description, can alter add-on grouping adding additional else if clauses.

c# regex string

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -