Finding all substrings in a string c# (Regex, Char Array?) -


this question has answer here:

i need identify substrings found in string such as:

"cityabcprocess test" or "cityabcprocess test"

to yield : [ "city/city", "abc", "process", "test" ]

  1. the first string in substring can lowercase or uppercase
  2. any substring recurring uppercase letters substring until lowercase letter or space found "abcprocess -> abc, abc process -> abc"
  3. if there uppercase letter followed lowercase letter substring until next uppercase letter.

can handled regex? or should convert strings character array , manually check these cases using indexing logic. lambda solution work here? best way go this?

pay no attention naysayers! isn't complicated regex. believe pattern should trick:

[a-z][a-z]+|[a-z]+\b|[a-z]+(?=[a-z])|[a-z]+

see here working demonstration. it's bunch of or's processed in order. here's breakdown:

  • [a-z][a-z]+ - word starts uppercase letter , followed lowercase letters
  • [a-z]+\b - word in uppercase (so include last uppercase letter excluded in following option)
  • [a-z]+(?=[a-z]) - word in uppercase, not including first uppercase letter of next word
  • [a-z]+ - word that's lowercase

for instance:

string input = "cityabcprocess test"; stringbuilder builder = new stringbuilder(); builder.append("[a-z][a-z]+"); builder.append("|"); builder.append("[a-z]+$"); builder.append("|"); builder.append("[a-z]+(?=[a-z])"); builder.append("|"); builder.append("[a-z]+"); foreach (match m in regex.matches(input, builder.tostring()))     {     console.writeline(m.value);     } 

Comments