|
9 | 9 | use Tempest\Support\Regex\InvalidPatternException; |
10 | 10 |
|
11 | 11 | use function Tempest\Support\Regex\get_all_matches; |
12 | | -use function Tempest\Support\Regex\get_first_match; |
| 12 | +use function Tempest\Support\Regex\get_match; |
| 13 | +use function Tempest\Support\Regex\get_matches; |
13 | 14 | use function Tempest\Support\Regex\matches; |
14 | 15 | use function Tempest\Support\Regex\replace; |
15 | 16 | use function Tempest\Support\Regex\replace_every; |
@@ -80,41 +81,87 @@ public function test_replace_every_with_invalid_pattern(): void |
80 | 81 | replace_every('April 15, 2003', ['/(\w+) (\d+), (\d+)' => '${1}1,$3']); |
81 | 82 | } |
82 | 83 |
|
83 | | - public function test_get_all_matches(): void |
| 84 | + public function test_get_match(): void |
84 | 85 | { |
85 | | - // simple pattern |
86 | | - $this->assertSame([['Hello', 'Hello']], get_all_matches('Hello world, Hello universe', '/Hello/')); |
| 86 | + $this->assertSame('10', get_match('10-abc', '/(?<id>\d+)-.*/', match: 'id')); |
| 87 | + $this->assertSame('10', get_match('10-abc', '/(\d+)-.*/', match: 1)); |
| 88 | + $this->assertSame('10', get_match('10-abc', '/(\d+)-.*/')); |
| 89 | + $this->assertSame('10-abc', get_match('10-abc', '/\d+-.*/', match: 0)); |
| 90 | + $this->assertSame(null, get_match('10-abc', '/\d+-.*/', match: 1)); |
87 | 91 |
|
88 | | - // named capture groups |
89 | | - $regex = '/(?<adjective>quick|lazy) (?<noun>brown|dog)/'; |
90 | | - $matches = get_all_matches('The quick brown fox, then the lazy dog', $regex); |
91 | | - $this->assertSame([ |
| 92 | + $this->assertSame( |
92 | 93 | [ |
93 | | - 'quick brown', |
94 | | - 'lazy dog', |
| 94 | + 'match' => "<href='https://tempestphp.com'>Tempest</href>", |
| 95 | + 'quote' => "'", |
| 96 | + 'href' => 'https://tempestphp.com', |
95 | 97 | ], |
96 | | - 'adjective' => [ |
97 | | - 'quick', |
98 | | - 'lazy', |
| 98 | + get_match("<href='https://tempestphp.com'>Tempest</href>", '/(?<match>\<href=(?<quote>[\"\'])(?<href>.+)\k<quote>\>(?:(?!\<href).)*?\<\/href\>)/g', match: [ |
| 99 | + 'match', |
| 100 | + 'quote', |
| 101 | + 'href', |
| 102 | + ]), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public function test_all_matches(): void |
| 107 | + { |
| 108 | + $this->assertSame( |
| 109 | + [ |
| 110 | + ['Hello'], |
| 111 | + ['Hello'], |
99 | 112 | ], |
100 | | - 1 => [ |
101 | | - 'quick', |
102 | | - 'lazy', |
| 113 | + get_all_matches('Hello world, Hello universe', '/Hello/'), |
| 114 | + ); |
| 115 | + |
| 116 | + $this->assertSame( |
| 117 | + [ |
| 118 | + [ |
| 119 | + 'match' => "<href='https://bsky.app'>Bluesky</href>", |
| 120 | + 'quote' => "'", |
| 121 | + 'href' => 'https://bsky.app', |
| 122 | + ], |
| 123 | + [ |
| 124 | + 'match' => "<href='https://x.com.com'>X</href>", |
| 125 | + 'quote' => "'", |
| 126 | + 'href' => 'https://x.com.com', |
| 127 | + ], |
103 | 128 | ], |
104 | | - 'noun' => [ |
105 | | - 'brown', |
106 | | - 'dog', |
| 129 | + get_all_matches( |
| 130 | + "<href='https://bsky.app'>Bluesky</href><href='https://x.com.com'>X</href>", |
| 131 | + '/(?<match>\<href=(?<quote>[\"\'])(?<href>.+?)\k<quote>\>(?:(?!\<href).)*?\<\/href\>)/g', |
| 132 | + matches: [ |
| 133 | + 'match', |
| 134 | + 'quote', |
| 135 | + 'href', |
| 136 | + ], |
| 137 | + ), |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + public function test_get_matches(): void |
| 142 | + { |
| 143 | + $this->assertSame([], get_matches('The quick brown fox, then the lazy dog', '/cat/', global: true)); |
| 144 | + |
| 145 | + $this->assertSame( |
| 146 | + [ |
| 147 | + 0 => '10-', |
| 148 | + 'id' => '10-', |
| 149 | + 1 => '10-', |
107 | 150 | ], |
108 | | - 2 => [ |
109 | | - 'brown', |
110 | | - 'dog', |
| 151 | + get_matches('10-abc', '/(?<id>\d+-)/'), |
| 152 | + ); |
| 153 | + |
| 154 | + $this->assertSame( |
| 155 | + [ |
| 156 | + [['foobar', 0]], |
| 157 | + [['foo', 0]], |
| 158 | + [['bar', 3]], |
111 | 159 | ], |
112 | | - ], $matches); |
| 160 | + get_matches('foobarbaz', '/(foo)(bar)/', global: true, flags: PREG_OFFSET_CAPTURE), |
| 161 | + ); |
113 | 162 |
|
114 | | - // No matches |
115 | | - $this->assertSame([], get_all_matches('The quick brown fox, then the lazy dog', '/cat/')); |
| 163 | + $this->assertSame([], get_matches('abcdef', '/^def/', global: true, offset: 3)); |
116 | 164 |
|
117 | | - // Mixed captures |
118 | 165 | $this->assertSame( |
119 | 166 | [ |
120 | 167 | [ |
@@ -146,26 +193,33 @@ public function test_get_all_matches(): void |
146 | 193 | 'eats', |
147 | 194 | ], |
148 | 195 | ], |
149 | | - get_all_matches('The quick brown fox, then the lazy dog eats', '/(?<adjective>quick|lazy) (?<noun>brown|dog) (?<action>jumps|eats)?/'), |
| 196 | + get_matches('The quick brown fox, then the lazy dog eats', '/(?<adjective>quick|lazy) (?<noun>brown|dog) (?<action>jumps|eats)?/', global: true), |
150 | 197 | ); |
151 | 198 |
|
152 | | - // Test flags |
153 | 199 | $this->assertSame( |
154 | 200 | [ |
155 | | - [['foobar', 0]], |
156 | | - [['foo', 0]], |
157 | | - [['bar', 3]], |
| 201 | + [ |
| 202 | + 'quick brown', |
| 203 | + 'lazy dog', |
| 204 | + ], |
| 205 | + 'adjective' => [ |
| 206 | + 'quick', |
| 207 | + 'lazy', |
| 208 | + ], |
| 209 | + 1 => [ |
| 210 | + 'quick', |
| 211 | + 'lazy', |
| 212 | + ], |
| 213 | + 'noun' => [ |
| 214 | + 'brown', |
| 215 | + 'dog', |
| 216 | + ], |
| 217 | + 2 => [ |
| 218 | + 'brown', |
| 219 | + 'dog', |
| 220 | + ], |
158 | 221 | ], |
159 | | - get_all_matches('foobarbaz', '/(foo)(bar)/', PREG_OFFSET_CAPTURE), |
| 222 | + get_matches('The quick brown fox, then the lazy dog', '/(?<adjective>quick|lazy) (?<noun>brown|dog)/', global: true), |
160 | 223 | ); |
161 | | - |
162 | | - $this->assertSame([], get_all_matches('abcdef', '/^def/', offset: 3)); |
163 | | - } |
164 | | - |
165 | | - public function test_match(): void |
166 | | - { |
167 | | - $match = get_first_match('10-abc', '/(?<id>\d+-)/')['id']; |
168 | | - |
169 | | - $this->assertSame('10-', $match); |
170 | 224 | } |
171 | 225 | } |
0 commit comments