@@ -53,6 +53,8 @@ final class RegexGroupParser
5353
5454 private const ASSERTIONS = ['#lookahead ' , '#lookbehind ' , '#negativelookahead ' , '#negativelookbehind ' ];
5555
56+ private const CONDITIONS = ['#namedcondition ' , '#absolutecondition ' , '#relativecondition ' , '#assertioncondition ' ];
57+
5658 private static ?RegexParser $ parser = null ;
5759
5860 /** @var array<string, ?RegexNode> */
@@ -291,8 +293,12 @@ private function walkRegexAst(
291293 true ,
292294 );
293295 $ parentGroup = $ group ;
294- } elseif (in_array ($ ast ->getId (), self ::NEGATIVE_ASSERTIONS , true )) {
295- // capturing groups in a negative assertion are never set when the whole pattern matches
296+ } elseif (
297+ in_array ($ ast ->getId (), self ::NEGATIVE_ASSERTIONS , true )
298+ || in_array ($ ast ->getId (), self ::CONDITIONS , true )
299+ ) {
300+ // capturing groups in a negative assertion are never set when the whole pattern matches,
301+ // capturing groups in a conditional subpattern are set only when its condition says so
296302 $ group = new RegexNonCapturingGroup (
297303 $ alternation ,
298304 true ,
@@ -515,6 +521,11 @@ private function walkGroupAst(
515521 return $ walkResult ;
516522 }
517523
524+ if (in_array ($ ast ->getId (), self ::CONDITIONS , true )) {
525+ // which branch of a conditional subpattern matches is not known, it might match an empty string
526+ return $ walkResult ->onlyLiterals (null )->numeric (TrinaryLogic ::createNo ());
527+ }
528+
518529 $ children = $ ast ->getChildren ();
519530 $ quantifiedLiterals = null ;
520531
@@ -785,7 +796,7 @@ private function repeatLiterals(array $literals, int $min, int $max): ?array
785796
786797 private function isMaybeEmptyNode (RegexNode $ node , string $ patternModifiers , bool &$ isNonFalsy ): bool
787798 {
788- if (in_array ($ node ->getId (), self ::ASSERTIONS , true )) {
799+ if (in_array ($ node ->getId (), self ::ASSERTIONS , true ) || in_array ( $ node -> getId (), self :: CONDITIONS , true ) ) {
789800 return true ;
790801 }
791802
0 commit comments