Skip to content

Commit ce899cd

Browse files
authored
feat: str match (#527)
1 parent 1e1447f commit ce899cd

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Tempest/Support/src/StringHelper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ public function replace(string|array $search, string|array $replace): self
347347
return new self(str_replace($search, $replace, $this->string));
348348
}
349349

350+
public function match(string $regex): array
351+
{
352+
preg_match($regex, $this->string, $matches);
353+
354+
return $matches;
355+
}
356+
350357
public function ld(mixed ...$ld): void
351358
{
352359
ld($this->string, ...$ld);

src/Tempest/Support/tests/StringHelperTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,11 @@ public function test_prepend(): void
260260
$this->assertTrue(str('bar')->prepend('foo')->equals('foobar'));
261261
$this->assertTrue(str('baz')->prepend('bar', 'foo')->equals('barfoobaz'));
262262
}
263+
264+
public function test_match(): void
265+
{
266+
$match = str('10-abc')->match('/(?<id>\d+-)/')['id'];
267+
268+
$this->assertSame('10-', $match);
269+
}
263270
}

0 commit comments

Comments
 (0)