Repository navigation
Description
Building on (but independent of) RegexOptions.AnyNewLine (#25598 which is in PR #124701) we can consider adding support for \R as recommended in Unicode Technical Standard 18 .. "It is strongly recommended that there be a regular expression meta-character, such as "\R", for matching all line ending characters and sequences listed above"
What it does.
\R is an escape shorthand for a character class, similar to \n but matching all newlines (\r, \v, \f, \n, \u0085, \u2028, \u2029) but with also with special behavior that it will match \r\n atomically. While #25598 makes ^, ., \Z, $ potentially match these, \R stands by itself. A common scenario would be: you have a pattern that contains \n and you want to make a minimal adjustment so that it works just as well against Windows line endings (\r\n): you just swap out \n for \R.
Reasons to potentially add
- Matching mixed newlines is easy to get wrong, even if you only care about
\r\nand\n. For example, if you want to normalize line endings:Regex.Replace("a\r\nb", @"\n", "|")will give youa\r|b; the arguably betterRegex.Replace("a\r\nb", @"[\r\n]", "|")would give youa||bbecause it's not treating\r\natomically. With\Ryou simply writeRegex.Replace("a\r\nb", @"\R", "|")and geta|bas expected. Even in .NET docs it suggests: "To handle the CR/LF character combination, include \r?$ in the regular expression pattern." (in the context of$but it's essentially the same error).\Rhandles all this in one fell swoop, including all the other kinds of newlines. - Porting code from other platforms. Currently it's supported by PCRE2 (so PHP, R, and some grep), Perl, Java 8 - although not the Rust
regexcrate (linear-time guarantee) or Python'sre. - Implementation is low risk and localized (lower
\Rto(?>\r\n|[\n\v\f\r\u0085\u2028\u2029])internally in parsing): it does not affect existing engines, codepaths or their performance. Proposed PR. Equal or faster in performance to the equivalent workaround and works fine in both regular andRightToLeftmodes. - Non breaking change.
\Rhas always thrown at parse time (exceptionEcmaScriptmode, which would not be changed) so no existing patterns will be affected.
Reasons to potentially not add
- Can't easily be supported by
NonBacktrackingengine. (Same limitation asAnyNewLine) - It won't be quite as fast as just
\nbecause that has special codepaths in the regex engines. If you only care about\n, then it's better to use\n, so there is a theoretical perf trap (but unlikely as\nis much more natural). If you care about more newlines, then\nisn't doing what you want anyway.
Activity
dotnet-policy-service commentedon Mar 10, 2026
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.
danmoseley commentedon Mar 12, 2026
@CyrusNajmabadi adding new supported syntax (even just an escape previously rejected) would impact Roslyn. The necessary changes seem quite small (scratch: danmoseley/roslyn@main...feature/regex-backslash-R-escape) but one wrinkle is this would only be legal on .NET 11. Your code aims to err on the side of not reporting errors if they might not be errors. So short of wiring through TFM somehow, I guess it would never squiggle \R, and always allow it as a completion.
Thoughts about Roslyn impact of adding this syntax?
CyrusNajmabadi commentedon Mar 13, 2026
Entirely fine to do this. We just won't report a live error if you do \R but target .Net 10 or earlier. That's totally ok.
CyrusNajmabadi commentedon Mar 13, 2026
Oh. \R makes me happy. Pity we can't backport it :D
danmoseley commentedon Mar 14, 2026
@stephentoub what are your thoughts
stephentoub commentedon Mar 14, 2026
How do we handle it with ECMAScript? We just treat "\R" the same as "R"?
What will we do with
\Rinside of character classes? Continue to throw "unrecognized escape sequence"? Throw a different error? Somehow allow it? Every other\escape sequence is allowed in a character class because it matches exactly one specific character, but\Ris different and it's not possible for a character class to start matching multiple characters (\r\n).But with some differences, right? Perl and Java don't include \v or \f, right?
Have we gotten requests for this, or is this being done because it's fun and we can?
danmoseley commentedon Mar 17, 2026
I'll let @CyrusNajmabadi speak to your first question.
[\R]on all of Perl, Java 8 and PCRE2 are documented as an error, as it would be on .NET and is in the prototype linked above.\Rall of on Perl, Java 8 and PCRE2 are documented to match the same set as\Rwould on .NET.No requests I'm aware of. The argument is essentially the same as AnyNewLine, which is that existing workarounds are clumsy.
For --
\n(or\r\n). Nobody will type all those out into the mess we lower \R into. There were 140 cases of things like[\r\n\u2028\u2029]in the LinguaFranca corpus (but that is large corpus)\r?\nwork fine (the fragility issues are fixed by AnyNewLine) but\Ris easier to read/type.Against --
\nthan\u2028in corpus above)\r?\nor(\r\n|\n)and then got altnerative answers later suggesting to use\Ras better after Java 8 came out. But, low usage suggests users don't care much. It's not in Javascript or Python.CyrusNajmabadi commentedon Mar 17, 2026
So we're in the same page, which question do you want me to answer?
danmoseley commentedon Mar 17, 2026
Sorry @CyrusNajmabadi I realize I misread the question and it was for me. I thought he was asking about tooling.
Anyway - now I wrote the for and against above, I think I've convinced myself that unlike AnyNewLine (now merged!) which helps avoid correctness pitfalls, \R is merely a shortcut for something users can generally write and reason about themselves (mostly for \r?\n). Assuming that's right then I think this issue can sit for future community comments or just be triaged away as the benefit isn't really there at this time.
Fix support for \R (generic newline) in regex parser\n\nRelated: dotn…
vladd commentedon Aug 24, 2026
I wonder why this was rejected.