Skip to content

Investigate adding capture support for RegexOptions.NonBacktracking #61904

Closed

Description

@stephentoub

As currently implemented, captures with RegexOptions.NonBacktracking are ignored, e.g. if you have an expression like abc(\w*)def, that's treated no differently from abc\w*def, and trying to access the resulting Match.Groups to access the \w* result will not be fruitful. While more challenging to do with a DFA, other production-quality engines (Rust, RE2, etc.) do support this implicitly. We should strongly investigate doing so for .NET 7.

cc: @olsaarik, @veanes, @joperezr, @danmoseley

Activity

  1. added this to the 7.0.0 milestone on Nov 22, 2021
  2. ghost added
    untriaged
    on Nov 22, 2021
  3. danmoseley commented on Nov 23, 2021

    @danmoseley
    Contributor

    Might this be an opportunity to make RegexOptions.ExplicitCapture default for the non backtracking engine, before anyone has taken dependency on it? I think this would mean adding RegexOptions.ImplicitCapture.

  4. stephentoub commented on Nov 23, 2021

    @stephentoub
    MemberAuthor

    Might this be an opportunity to make RegexOptions.ExplicitCapture default for the non backtracking engine

    That's the exact opposite of what I'm suggesting happen.

  5. veanes commented on Nov 23, 2021

    @veanes
    Contributor

    I think that one can maintain capture nodes in the SymbolicRegexNode AST that record the name and associate input position during the third phase of the matcher that keep track of the start/end position during matching. Currently this information is simply ignored as all capture nodes are essentially eliminated during the translation from RegexNode to SymbolicRegexNode (there are some tricky cases though e.g. in potential deep nesting of concatenations where captures are eliminated/flattened currently for perf reasons).
    This mostly affects the 3rd phase of the matcher (unless there are watchdogs in which case the initial phase should already keep track of these, e.g. for input m(arg)us that would have watchdog #6 but also capture 1). I believe this would not add much more states and should not incur a big overhead and would not affect derivative semantics in general, but is a tricky change in general (mainly due to various optimizations).

  6. removed
    untriaged
    on Dec 10, 2021
  7. stephentoub commented on Feb 25, 2022

    @stephentoub
    MemberAuthor

    This has been implemented.

  8. ghost locked as resolved and limited conversation to collaborators on Mar 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions