Friday, July 27, 2007

RegexOptions.Multiline and WriteLine, AppendLine, etc.

The Multiline Regex option changes the way "^" and "$" work so that they match the beginning and the end of each line in the input string. Good for quick searches of a string in a list of newline separated strings.

The WriteLine and AppendLine and other .Net text related methods that end in Line append at the end of the input string an Environment.NewLine. This is \r\n in Windows and \n in Linux based systems. But, RegexOptions.Multiline only works on... you guessed it... \n aka new line, ignoring the good old carriage return altogether.

The solutions are: either create the string that contains the lines by adding manually \n and not using *Line, or change the regular expression to something like "^something\r?$".


Anonymous said...

until it's fixed, a workaround is a zero-width assertion (?=\r?$) instead of just $

Siderite said...

Is there an optimisation reason for using that syntax?