-1

I am trying to use a regex in c# to parse a multi line file ( Currently just in a string ) And this part seems to be where the problem is (.*?\n) it will split the .v/L1 and .v/L2 however when I put a nl between it fails the input file will look something like this.

 MSG
 .v/1L
 .v/2L
 .some other data
 .and so on
 ENDMSG

And this is part of the c# code

  string nl = new string(new char[] { '\u000A' });
  string pattern = @"(?<group>((?<type>MSG\n)(.*?\n)(?<end>ENDMSG\n)))";
  string input = @" MSG" + nl + ".v/1L.v/2L" + nl + "ENDMSG" + nl;
 // The Line below doesn't work  
 //string input = @" MSG" +nl+ ".v/1L" +nl+ ".v/2L" +nl+ "ENDMSG" + nl;
  RegexOptions options = RegexOptions.Multiline;

  foreach (Match m in Regex.Matches(input, pattern, options))
        {
           Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }

And this is the output of the one that works:

Starting RegEx !
RegEx : (?<group>((?<type>MSG\n)(.*?\n)(?<end>ENDMSG\n)))
'MSG
.v/1L.v/2L   << Should be split into 2 when adding a \n between
ENDMSG
' found at index 1.
Executing finally block.
Dan Miller
  • 37
  • 6

1 Answers1

-2

If you specify RegexOptions.Multiline then you can use ^ and $ to match the start and end of a line, respectively.

If you don't wish to use this option, remember that a new line may be any one of the following: \n, \r, \r\n, so instead of looking only for \n, you should perhaps use something like: [\n\r]+, or more exactly: (\n|\r|\r\n).

Vishal
  • 192
  • 11