2

Possible Duplicate:
How do I split a string by strings and include the delimiters using .NET?

I'm trying to split a paragraph into sentences, using the String.split(_separators) method, but I want it to return my separators too.

static char[] _separators = { '.', '?', '!' };     
string[] sentences = parag.Split(_separators);

lets say my parag is: "Thank you. For helping me!"

It will return

  • Thank you
  • For helping me

I want it to return something like this

  • Thank you
  • .
  • For helping me
  • !

I never used regex, is there a way this can be done using regex?

I tried regex and here how it works.

  static string _separators = @"(\.)+|(\?)+|(\!)+|(\,)+|(\;)+";
  string [] sentences = Regex.Split(phrase, pattern);

PS: I added the + to group the same separator together

Community
  • 1
  • 1
raym0nd
  • 3,172
  • 7
  • 36
  • 73
  • 5
    See: http://stackoverflow.com/questions/3032072/c-string-split-returning-list-of-strings-and-list-of-delimiters – Anthony Pegram Jul 13 '11 at 20:43
  • 3
    Another one: http://stackoverflow.com/questions/2484919/how-do-i-split-a-string-by-strings-and-include-the-delimiters-using-net – Jason Down Jul 13 '11 at 20:45
  • @all thanks guys, i dont know why I didnt find these before I posted my question. – raym0nd Jul 13 '11 at 20:50
  • @Anthony Pegram, @Jason Down, why do you give the answer in the comments? he was not able to choose – Mediator Jul 13 '11 at 20:50
  • 2
    @simply denis, *our comments are not answers.* Our comments simply told the user *where the answers are.* If he finds one or more of those answers useful, he can vote them up. – Anthony Pegram Jul 13 '11 at 20:52
  • 1
    @raymond, some of those characters need to be escaped. Try (untested) `@"(\.|\?|!|,|;)"` – Anthony Pegram Jul 13 '11 at 20:58
  • @Anthony Pegram, but then we will see that the answer is, only if people will go to this page, and not opening it to see what the answer is no. As that is not correct. IMHO – Mediator Jul 13 '11 at 20:59
  • @simply denis, that is the way this site works. Duplicates are tagged and with enough votes, the question is closed and links are added to the original text of the question itself. If you disagree, feel free to voice your concern on [meta]. – Anthony Pegram Jul 13 '11 at 21:01

0 Answers0