UB: C#'s Regex.Match returns whole string instead of part when matching

前端 未结 3 1633
小蘑菇
小蘑菇 2021-01-19 07:21

Attention! This is NOT related to Regex problem, matches the whole string instead of a part


Hi all. I try to do

Match y = Reg         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-19 08:13

    My test regex was different from any others in the project's scope (thats what happens when Perl guy comes to C#), as it had no lookaheads/lookbehinds. So this discovery took some time.

    Now, why we should call Regex behaviour undocumented, not undefined:

    let's do some matches against "1.234567890".

    • PCRE-like syntax: (.)\.2345678
    • lookahead syntax: (.)(?=\.\d)

    When you're doing a normal match, the result is copied from whole matched part of line, no matter where you've put the parentesizes; in case of lookaheads present, anything that did not belongs to them is copied.

    So, the matches will return:

    • PCRE: 1.2345678 (at 2300, this looks like original string and I start yelling here at SO)
    • lookahead: 1

提交回复
热议问题