c# - Regex: how to get words, spaces and punctuation from string -


basically want iterate through sentence, example:

string sentence = "how day - andrew, jane?"; string[] separated = separatesentence(sentence); 

separated output following:

[1] = "how"

[2] = " "

[3] = "was"

[4] = " "

[5] = "your"

[6] = " "

[7] = "day"

[8] = " "

[9] = "-"

[10] = " "

[11] = "andrew"

[12] = ","

[13] = " "

[14] = "jane"

[15] = "?"

as of can grab words, using "\w(?<!\d)[\w'-]*" regex. how separate sentence smaller parts, according output example?

edit: string doesn't have of following:

  • i.e.

  • solid-form

  • 8th, 1st, 2nd

check out:

        string pattern = @"^(\s+|\d+|\w+|[^\d\s\w])+$";         string input = "how 7 day - andrew, jane?";          list<string> words = new list<string>();          regex regex = new regex(pattern);          if (regex.ismatch(input))         {             match match = regex.match(input);              foreach (capture capture in match.groups[1].captures)                 words.add(capture.value);         } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -