xref: /PHP-7.4/ext/pcre/tests/bug79257.phpt (revision 3a515309)
1--TEST--
2Bug #79257: Duplicate named groups (?J) prefer last alternative even if not matched
3--FILE--
4<?php
5
6preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', 'foo', $matches);
7var_dump($matches);
8preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', 'foo', $matches,
9    PREG_UNMATCHED_AS_NULL);
10var_dump($matches);
11preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', 'foo', $matches,
12    PREG_OFFSET_CAPTURE);
13var_dump($matches);
14preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', 'foo', $matches,
15    PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE);
16var_dump($matches);
17
18preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', 'foobaz', $matches);
19var_dump($matches);
20preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', 'foobaz', $matches,
21    PREG_UNMATCHED_AS_NULL);
22var_dump($matches);
23preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', 'foobaz', $matches,
24    PREG_OFFSET_CAPTURE);
25var_dump($matches);
26preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', 'foobaz', $matches,
27    PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE);
28var_dump($matches);
29
30?>
31--EXPECT--
32array(3) {
33  [0]=>
34  string(3) "foo"
35  ["g"]=>
36  string(3) "foo"
37  [1]=>
38  string(3) "foo"
39}
40array(4) {
41  [0]=>
42  string(3) "foo"
43  ["g"]=>
44  string(3) "foo"
45  [1]=>
46  string(3) "foo"
47  [2]=>
48  NULL
49}
50array(3) {
51  [0]=>
52  array(2) {
53    [0]=>
54    string(3) "foo"
55    [1]=>
56    int(0)
57  }
58  ["g"]=>
59  array(2) {
60    [0]=>
61    string(3) "foo"
62    [1]=>
63    int(0)
64  }
65  [1]=>
66  array(2) {
67    [0]=>
68    string(3) "foo"
69    [1]=>
70    int(0)
71  }
72}
73array(4) {
74  [0]=>
75  array(2) {
76    [0]=>
77    string(3) "foo"
78    [1]=>
79    int(0)
80  }
81  ["g"]=>
82  array(2) {
83    [0]=>
84    string(3) "foo"
85    [1]=>
86    int(0)
87  }
88  [1]=>
89  array(2) {
90    [0]=>
91    string(3) "foo"
92    [1]=>
93    int(0)
94  }
95  [2]=>
96  array(2) {
97    [0]=>
98    NULL
99    [1]=>
100    int(-1)
101  }
102}
103array(6) {
104  [0]=>
105  string(6) "foobaz"
106  ["g"]=>
107  string(3) "foo"
108  [1]=>
109  string(3) "foo"
110  [2]=>
111  string(0) ""
112  ["h"]=>
113  string(3) "baz"
114  [3]=>
115  string(3) "baz"
116}
117array(6) {
118  [0]=>
119  string(6) "foobaz"
120  ["g"]=>
121  string(3) "foo"
122  [1]=>
123  string(3) "foo"
124  [2]=>
125  NULL
126  ["h"]=>
127  string(3) "baz"
128  [3]=>
129  string(3) "baz"
130}
131array(6) {
132  [0]=>
133  array(2) {
134    [0]=>
135    string(6) "foobaz"
136    [1]=>
137    int(0)
138  }
139  ["g"]=>
140  array(2) {
141    [0]=>
142    string(3) "foo"
143    [1]=>
144    int(0)
145  }
146  [1]=>
147  array(2) {
148    [0]=>
149    string(3) "foo"
150    [1]=>
151    int(0)
152  }
153  [2]=>
154  array(2) {
155    [0]=>
156    string(0) ""
157    [1]=>
158    int(-1)
159  }
160  ["h"]=>
161  array(2) {
162    [0]=>
163    string(3) "baz"
164    [1]=>
165    int(3)
166  }
167  [3]=>
168  array(2) {
169    [0]=>
170    string(3) "baz"
171    [1]=>
172    int(3)
173  }
174}
175array(6) {
176  [0]=>
177  array(2) {
178    [0]=>
179    string(6) "foobaz"
180    [1]=>
181    int(0)
182  }
183  ["g"]=>
184  array(2) {
185    [0]=>
186    string(3) "foo"
187    [1]=>
188    int(0)
189  }
190  [1]=>
191  array(2) {
192    [0]=>
193    string(3) "foo"
194    [1]=>
195    int(0)
196  }
197  [2]=>
198  array(2) {
199    [0]=>
200    NULL
201    [1]=>
202    int(-1)
203  }
204  ["h"]=>
205  array(2) {
206    [0]=>
207    string(3) "baz"
208    [1]=>
209    int(3)
210  }
211  [3]=>
212  array(2) {
213    [0]=>
214    string(3) "baz"
215    [1]=>
216    int(3)
217  }
218}
219