NET 11 Adds RegexOptions.AnyNewLine to Stop Stray Carriage Returns in Regex Matches
In .NET 11, a new RegexOptions.AnyNewLine flag makes the anchors ^, $, and \Z and the dot treat \r\n, \r, and Unicode line breaks as line endings, not just \n. Previously, .NET's regex engine recognized only \n, so matches on Windows-formatted text often kept a trailing \r that broke equality checks. The common workaround of using [^\r\n]*\r?$ handles \r\n but misses bare \r line endings. The author demonstrated that the flag correctly parses mixed line endings in a config example where the older patterns dropped or merged entries. The flag works with standard Regex calls, RegexOptions.Compiled, and GeneratedRegex, but it requires .NET 11 and fails to build on .NET 10. .NET 11 was at release candidate 2 at the time of writing, with general availability expected in November.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in