0

I have the following string that I want to split on the "\n" but I need to remove/replace all of the occurances of multiple "\n\n";

Here is my sample string:

"\n01:05 PM\n901\nNew York Mets \n\n\n\n\n\n\n\n\n \n+125\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"

So that I end up with an array like 1:05 PM 901 New York Mets +125

I've tried,

 string adjRow = row.InnerText.Replace("\n", "");

Is there a single Regex Pattern that will work?

Added the following and seems to work.

    RegexOptions options = RegexOptions.None;
                    Regex regex = new Regex("[\n]{2,}", options);
                    string adjRow = regex.Replace(tempRow, "\n");

Here is the result,

"\n01:05 PM\n901\nNew York Mets \n \n+128\n \nOv\n8½\n-113\no\n"

Trey Balut
  • 1,355
  • 3
  • 19
  • 39
  • 2
    Replace `\n+` with `\n` – Pushpesh Kumar Rajwanshi Mar 07 '19 at 15:18
  • Actually, `\n{2,}` makes more sense, why match a single newline if you need to keep it as is. – Wiktor Stribiżew Mar 07 '19 at 15:22
  • I'm not following. You want _"an array like 1:05 PM 901 New York Mets +125"_ and you want to _"split on the '\n'"_. I don't see any array, and I don't see you "splitting". My take... do it in two passes. Replace every "\n" with a space, and then use a regex to replace occurrences of multiple spaces with a single space. – Flydog57 Mar 07 '19 at 15:23
  • Ignoring the regex, you can use `Split(new string[] {"\n"}, StringSplitOptions.RemoveEmpty)`. (I'm not near a computer right now so maybe you have to change some stuff) – fstam Mar 07 '19 at 22:16

0 Answers0