Skip to content

Regex conditional groups are broken if no "else" expression is given #24894

Closed

Description

@kendfrey

Scenario:

Match match = Regex.Match("abc", "(?(?=x)bx)");
Console.WriteLine("Success: {0}", match.Success);
Console.WriteLine("Value: \"{0}\"", match.Value);
Console.WriteLine("Index: {0}", match.Index);

Output:

Success: True
Value: ""
Index: 1

Expected result:

I don't know. If leaving out the "else" expression isn't supported, it should throw an exception. Otherwise, matching anywhere seems the most reasonable option. In any case, if the condition fails, the result should not depend on the contents of the "if" expression.

Additional notes:

https://stackoverflow.com/q/48574288/785745

Activity

  1. danmoseley commented on Feb 3, 2018

    @danmoseley
    Contributor

    Same in .NEt Framework.

  2. danmoseley commented on Feb 4, 2018

    @danmoseley
    Contributor

    At first it seems fine to not have an "else". For example Regex.Match("abc", "(?(?=bc)b)"); returns b and the same in PCRE and Perl.

    In each case if the "else" is missing it seems to be equivalent to an empty pattern. ie., it always matches, but has no matching groups. In this case of course the assertion (?=x) is false so this is the result.

    However if there is anything else in the pattern, that breaks down. Eg., although Regex.Match("abc", "(?(?=x)bx|a)bc") matches abc, Regex.Match("abc", "(?(?=x)bx)abc") fails to match at all.

    As far as I can tell the behavior is the same as Perl. PCRE however does get abc using (?(?=x)bx)abc.

    So I guess, always have an "else" otherwise you get odd behavior. If you are motivated, you could dig into the code to figure out "why" but I'm not sure this could be changed and still be confident of not breaking someone. (For example clearly we couldn't now make it throw if you were missing "else")

    If you agree, we can close this.

    cc @ViktorHofer

  3. kendfrey commented on Feb 4, 2018

    @kendfrey
    Author

    If this is intended, and there is some explanation/documentation of what the rationale behind the behaviour is, I'd be fine with closing it. It doesn't make sense to close it if it's a bug, though.

  4. ViktorHofer commented on Feb 5, 2018

    @ViktorHofer
    Member

    Thanks @kendfrey for the report. Thanks @danmosemsft for the thorough analysis. The documentation mentions that the else clause is optional: https://docs.microsoft.com/en-us/dotnet/standard/base-types/alternation-constructs-in-regular-expressions#Conditional_Expr

    To make it absolutely clear we could add the phrasing that if no else clause is provided and the condition doesn't match, the pattern matches with an empty capture. I would also add the note that if something follows after the conditional match, no else clause is provided and the condition fails, the whole match fails.

    cc @rpetrusha @mairaw

  5. kendfrey commented on Feb 5, 2018

    @kendfrey
    Author

    I don't quite understand what you mean by:

    if something follows after the conditional match, no else clause is provided and the condition fails, the whole match fails.

    Does this mean that (?(?=x)a)a would fail to match "a"? That doesn't seem right.

  6. ViktorHofer commented on Feb 5, 2018

    @ViktorHofer
    Member

    Does this mean that (?(?=x)a)a would fail to match "a"? That doesn't seem right.

    This pattern fails to match:

    > var x = System.Text.RegularExpressions.Regex.Match("abc", "(?(?=x)bx|a)bc");
    > x.Success
    false

    Your pattern matches and captures a successfully.

    > var x = System.Text.RegularExpressions.Regex.Match("abc", "(?(?=x)a)a");
    > x.Success
    true
    > x.Captures[0].Value
    "a"
  7. kendfrey commented on Feb 5, 2018

    @kendfrey
    Author

    That doesn't make sense to me because that changes the behaviour of the conditional group based on context around it. Also, currently (?(?=x)a)a does match "a".

  8. ViktorHofer commented on Feb 5, 2018

    @ViktorHofer
    Member

    You are right, something is wrong here:

    > System.Text.RegularExpressions.Regex.Match("abc", "(?(?=x)a)abc").Success
    true
    > System.Text.RegularExpressions.Regex.Match("abc", "(?(?=x)b)abc").Success
    false
  9. danmoseley commented on Feb 5, 2018

    @danmoseley
    Contributor

    It seems to me we don't fully understand what the engine is doing here enough to document it confidently. It would probably take someone stepping through the code. Either to figure out a fix (if it's not too breaking) or understand well enough to document it.

  10. ViktorHofer commented on Feb 5, 2018

    @ViktorHofer
    Member

    I agree. I will step through the code to get a better understanding.

  11. kendfrey commented on Feb 5, 2018

    @kendfrey
    Author

    My working hypothesis is as described in the initial Stack Overflow question:

    It seems that a conditional group without an "else" expression will instead create a lookahead from the first character of the "if" expression and use that as the "else".

    So (?(?=abc)xyz) is treated as (?(?=abc)xyz|(?=x)).

    I don't know if this is accurate, but it does match everything I've seen so far.

    I did take a look at the code, but I didn't understand it well enough to spot anything.

  12. ViktorHofer commented on Feb 5, 2018

    @ViktorHofer
    Member

    Tree parsing is correct (the root capture is the global one):

    Tree "(?(?=x)bx|a)bc":
    
    Capture
     Concatenate
      Testgroup
       Require
        One (x)
       Multi (bx)
       One (a)
      Multi (bc)
    
    Tree "(?(?=x)a)abc":
    
    Capture
     Concatenate
      Testgroup
       Require
        One (x)
       One (a)
      Multi (abc)
    
  13. self-assigned this
    on Mar 27, 2018
  14. kendfrey commented on Jun 11, 2018

    @kendfrey
    Author

    @MohammadHamdyGhanem Having "" cause the match to fail is the opposite of how it normally works, which would be extremely confusing behaviour.

    Otherwise, that looks proper (and is how I implemented it in my engine).

  15. kendfrey commented on Jun 12, 2018

    @kendfrey
    Author

    @MohammadHamdyGhanem

    Could you explain? I get confused. What "looks proper"?

    Essentially just the Principle of Least Astonishment. It's what how I would expect it to behave.

    Is there a link to your engine?

    It's written in TypeScript which may not be of much use to you. It's just intended to bring the features of .NET's regex to JS. (Still a WIP)

  16. removed their assignment
    on Aug 18, 2018
  17. transferred this issue fromdotnet/corefxon Jan 31, 2020
  18. added this to the Future milestone on Jan 31, 2020
  19. stephentoub commented on Jan 13, 2022

    @stephentoub
    Member

    The behavior is supposed to be that a missing else is treated identically to an empty else. That's generally the case but there are some corner- case bugs that made it confusing. This is fixed for .NET 7 as of #63427.

  20. added and removed
    documentation
    on Jan 13, 2022
  21. ghost locked as resolved and limited conversation to collaborators on Feb 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.RegularExpressionsbughelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions