-2

I want to split the string "This is regarding the problem of {pro} in {statement}"

I want to get output is

This is regarding the problem of
{pro}
in
{statement}
  • As a string with new lines or as an array of strings? – Jason W Mar 17 '15 at 02:32
  • It's unclear what you're asking. Do you always want to split after the words `of` and `in`, and the words that immediately follow those? Do you always want to split on the 25th, 28th, 33rd, and 44th letters (no, I didn't count exactly)? – mason Mar 17 '15 at 02:32
  • No. I want split the when found { and also want value within {}. – user4678815 Mar 17 '15 at 02:35
  • string will be randomly.Example "{pro} of this one in {statement} – user4678815 Mar 17 '15 at 02:35
  • And while you took the time to write the question, it did not come to mind to google the words 'split string c#' (which is basically your title)? If there's less than 100 relevant results I'd be extremely surprised. – Pierre-Luc Pineault Mar 17 '15 at 02:36
  • I searching in google. I can not find the relevant results. Just post to get logic from someone help. – user4678815 Mar 17 '15 at 02:38
  • 1
    [Well](https://msdn.microsoft.com/en-us/library/System.String.Split%28v=vs.110%29.aspx) [there's](http://www.dotnetperls.com/split) [a](http://csharp.net-informations.com/string/csharp-string-split.htm) [lot](http://stackoverflow.com/questions/8928601/how-can-i-split-a-string-with-a-string-delimiter) [of](https://msdn.microsoft.com/en-us/library/ms228388.aspx) [em](http://stackoverflow.com/questions/7559121/c-sharp-splitting-strings). Looks like you need to sharpen your Googling skills. – Pierre-Luc Pineault Mar 17 '15 at 02:43

2 Answers2

2

You could try this regex:

([^{]+|{[^}]*})

It matches each group of characters which are defined by either:

  • A sequence of characters (at least one), none of which are {; or
  • A { character, followed by any number of characters which are not }, all followed by }
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • Your regex is very helpful. Thank you very much. I can continue my work. – user4678815 Mar 17 '15 at 02:54
  • 1
    `({[^}]*})` seems to do the same thing and creates less empty strings, thus performs slightly better (7.1 sec vs 11.4 sec for 5 million repetitions) – learningcs Mar 17 '15 at 02:56
  • That will only pull out the {} and their contents? The regex I posted also matches each section of text in between and around the {} sections – Blorgbeard Mar 17 '15 at 02:59
  • I definitely didn't think about performance though, there may be faster solutions than mine for sure – Blorgbeard Mar 17 '15 at 03:01
  • 2
    @Blorgbeard It will return the 2 bracketed elements running Regex.Matches, or 5 elements (4 split elements and 1 empty string) running Regex.Split -- running yours through Regex.Matches returns the 4 lines but removes the curly braces (which I assume OP wanted left in based on the question) and Regex.Split returns 9 elements (5 of which are empty strings) – learningcs Mar 17 '15 at 03:13
0

Here's a simple regex that will insert a new line before and after your token matches by overriding the evaluator:

string output = Regex.Replace(input, @"{\S+}", m => string.Format(@"{1}{0}{1}", m.Value, '\n'));

The output variable will have a newline after. You can then just do a string split if you need the output in an array of strings.

string[] lines = output.Split('\n');
Jason W
  • 13,026
  • 3
  • 31
  • 62