You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regex conditional groups are broken if no "else" expression is given #24894
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.
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 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.
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.
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)adoes match "a".
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.
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.
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.
Activity
danmoseley commentedon Feb 3, 2018
Same in .NEt Framework.
danmoseley commentedon Feb 4, 2018
At first it seems fine to not have an "else". For example
Regex.Match("abc", "(?(?=bc)b)");returnsband 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")matchesabc,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
abcusing(?(?=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
kendfrey commentedon Feb 4, 2018
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.
ViktorHofer commentedon Feb 5, 2018
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
kendfrey commentedon Feb 5, 2018
I don't quite understand what you mean by:
Does this mean that
(?(?=x)a)awould fail to match"a"? That doesn't seem right.ViktorHofer commentedon Feb 5, 2018
This pattern fails to match:
Your pattern matches and captures
asuccessfully.kendfrey commentedon Feb 5, 2018
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)adoes match"a".ViktorHofer commentedon Feb 5, 2018
You are right, something is wrong here:
danmoseley commentedon Feb 5, 2018
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.
ViktorHofer commentedon Feb 5, 2018
I agree. I will step through the code to get a better understanding.
kendfrey commentedon Feb 5, 2018
My working hypothesis is as described in the initial Stack Overflow question:
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.
ViktorHofer commentedon Feb 5, 2018
Tree parsing is correct (the root capture is the global one):
kendfrey commentedon Jun 11, 2018
@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).
kendfrey commentedon Jun 12, 2018
@MohammadHamdyGhanem
Essentially just the Principle of Least Astonishment. It's what how I would expect it to behave.
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)
stephentoub commentedon Jan 13, 2022
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.