Help with Split With#

I have a long string that has newline transitions, question marks, exclamation marks, dots. I need to get an array of strings that will divide by the rule,

String[] lines = myText.Split(new char[] { '\r', '\n', '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);

This solution divides everything correctly, but the problem is that it removes punctuation marks. I would like to leave '.', '!', '?' here are these characters and delete the newline transitions \r, \n. Example:

Lorem ipsum, dolor sit amet, elit. Nullam! Faucibus congue?
Phasellus molestie, orci
Ut aliquam nulla tristique nec?
Nec bibendum tortor sodales!

Result

1-Lorem ipsum, dolor sit amet, elit.
2-Nullam!
3-Faucibus congue?
4-Phasellus molestie, orci
5-Ut aliquam nulla tristique nec?
6-Nec bibendum tortor sodales!

Thank you all very much! All answers worker:)

 4
Author: xom9ikk, 2018-01-18

2 answers

First, we prepare the text by adding the characters !?. \n.

var prepair = myText.Replace(".",".\n").Replace("!","!\n").Replace("?","?\n");

String[] lines = prepair.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
 11
Author: Дмитрий Полянин, 2018-01-18 16:12:52

Let's divide the text using a regular expression:

string text = "Lorem ipsum, dolor sit amet, elit. Nullam! Faucibus congue?\r\nPhasellus molestie, orci\r\nUt aliquam nulla tristique nec?\r\nNec bibendum tortor sodales!";
string pattern = @"((?<=[.?!])|\r?\n) *";
foreach (string s in Regex.Split(text, pattern).Where(s => !string.IsNullOrWhiteSpace(s)))
    Console.WriteLine(s);

However, you will have to delete the empty lines manually (I do this with Where), since Regex.Split() does not have an analog StringSplitOptions.RemoveEmptyEntries

Here the pattern describes the format of the separator:

(?<=[.?!]) - positive backward view, i.e. we are looking for one of the characters . or ? or !, but they are not included in the separator themselves, they will be included in the part before the separator

\r?\n - line feed, supports options like \r\n, and just \n

| - "or", i.e. the separator satisfies either one pattern or the other

* - and any number of spaces, they will belong to the separator and will not be included in the result

 3
Author: Андрей NOP, 2018-01-18 17:38:25