Reputation: 165
How can I split a text into words in .NET, returning both the words and their separators and punctuation marks.
Input:
"Hello world! What's up?"
Output: 'Hello', ' ', 'world', '!', ' ','What's',' ','up','?'
Upvotes: 0
Views: 113
Reputation: 2940
var output = Regex.Split(input, "([^A-Z0-9])", RegexOptions.IgnoreSpace);
Upvotes: 0
Reputation: 67968
(?=[^\w'])|(?<=[^\w'])
You can split by this.See demo.
https://regex101.com/r/iJ7bT6/15
Upvotes: 4