Skip to content

Support \R (generic newline) in regular expressions #125363

Closed as not planned
Closed as not planned

Description

@danmoseley

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\n and \n. For example, if you want to normalize line endings: Regex.Replace("a\r\nb", @"\n", "|") will give you a\r|b; the arguably better Regex.Replace("a\r\nb", @"[\r\n]", "|") would give you a||b because it's not treating \r\n atomically. With \R you simply write Regex.Replace("a\r\nb", @"\R", "|") and get a|b as 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). \R handles 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 regex crate (linear-time guarantee) or Python's re.
  • Implementation is low risk and localized (lower \R to (?>\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 and RightToLeft modes.
  • Non breaking change. \R has always thrown at parse time (exception EcmaScript mode, which would not be changed) so no existing patterns will be affected.

Reasons to potentially not add

  • Can't easily be supported by NonBacktracking engine. (Same limitation as AnyNewLine)
  • It won't be quite as fast as just \n because 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 \n is much more natural). If you care about more newlines, then \n isn't doing what you want anyway.

Activity

  1. added
    untriaged
    on Mar 10, 2026
  2. dotnet-policy-service commented on Mar 10, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
    See info in area-owners.md if you want to be subscribed.

  3. danmoseley commented on Mar 12, 2026

    @danmoseley
    ContributorAuthor

    @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?

  4. CyrusNajmabadi commented on Mar 13, 2026

    @CyrusNajmabadi
    Contributor

    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.

  5. CyrusNajmabadi commented on Mar 13, 2026

    @CyrusNajmabadi
    Contributor

    Oh. \R makes me happy. Pity we can't backport it :D

  6. danmoseley commented on Mar 14, 2026

    @danmoseley
    ContributorAuthor

    @stephentoub what are your thoughts

  7. stephentoub commented on Mar 14, 2026

    @stephentoub
    Member

    Non breaking change. \R has always thrown at parse time (exception EcmaScript mode, which would not be changed) so no existing patterns will be affected.

    How do we handle it with ECMAScript? We just treat "\R" the same as "R"?

    What will we do with \R inside 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 \R is different and it's not possible for a character class to start matching multiple characters (\r\n).

    Currently it's supported by PCRE2 (so PHP, R, and some grep), Perl, Java 8 - although not the Rust regex crate (linear-time guarantee) or Python's re.

    But with some differences, right? Perl and Java don't include \v or \f, right?

    what are your thoughts

    Have we gotten requests for this, or is this being done because it's fun and we can?

  8. danmoseley commented on Mar 17, 2026

    @danmoseley
    ContributorAuthor

    I'll let @CyrusNajmabadi speak to your first question.

    What will we do with \R inside of character classes?

    [\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.

    But with some differences, right? Perl and Java don't include \v or \f, right?

    \R all of on Perl, Java 8 and PCRE2 are documented to match the same set as \R would on .NET.

    Have we gotten requests for this, or is this being done because it's fun and we can?

    No requests I'm aware of. The argument is essentially the same as AnyNewLine, which is that existing workarounds are clumsy.

    For --

    • Practical way to capture all 7 newline flavors, not just \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)
    • If you don't care about those, does \r?\n work fine (the fragility issues are fixed by AnyNewLine) but \R is easier to read/type.
    • Completes our implementation of the Unicode regex newline standard.
    • Slightly more patterns from Java (and Perl) that can be reused unchanged.

    Against --

    • No precedent for adding new .NET Regex pattern constructs, although tooling is already lax.
    • Cognitive load for those readers unfamiliar.
    • Real patterns rarely care about the higher order newlines (About 100x more \n than \u2028 in corpus above)
    • Usage on other platforms seems low (I looked at the LinguaFranca corpus). Some of that may be because patterns were written before Java 8. These two SO questions (one, two) which suggest variants of \r?\n or (\r\n|\n) and then got altnerative answers later suggesting to use \R as better after Java 8 came out. But, low usage suggests users don't care much. It's not in Javascript or Python.
  9. CyrusNajmabadi commented on Mar 17, 2026

    @CyrusNajmabadi
    Contributor

    I'll let @CyrusNajmabadi speak to your first question.

    So we're in the same page, which question do you want me to answer?

  10. danmoseley commented on Mar 17, 2026

    @danmoseley
    ContributorAuthor

    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.

  11. removed
    untriaged
    on Aug 16, 2026
  12. added this to the Future milestone on Aug 16, 2026
  13. vladd commented on Aug 24, 2026

    @vladd

    I wonder why this was rejected.

  14. locked and limited conversation to collaborators on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions