A tool that makes it easier to work with regex groups. Mapped regex group to php-classes, because array is sucks.
composer require ta-tikoma/bog-jug
- Create a class for regex descriptions.
- Use property attributes for defined regex group:
#[Group('...')]
for group body;#[After]
and#[Before]
for defined outside symbols. - If you need to indicate the count of a group, use attributes like
#[ZeroOrOne]
,#[ZeroOrMore]
and others from the namespaceBogJug\Attributes\Count
. - Finally, you can add regex flags via the class attributes; for example:
#[SingleLine]
. - Now create instance of class BogJug and use one of two base methods:
- Method
->one($regex, $text)
to find the first value equal to regex; analogue:preg_match
. - Method
->many($regex, $text)
to get all values equal regex; analog:preg_match_all
.
- Method
<?php
declare(strict_types=1);
namespace tests\Data;
use BogJug\Attributes\Flags\SingleLine;
use BogJug\Attributes\Regex\After;
use BogJug\Attributes\Regex\Group;
#[SingleLine]
final class TinWoodman
{
public function __construct(
#[Group('head'), After('.*')]
public readonly string $noggin,
#[Group('arms'), After('.*')]
public readonly string $upperLimbs,
#[Group('legs'), After('.*')]
public readonly string $lowerLimbs,
#[Group('body'), After('.*')]
public readonly string $torso,
#[Group('heart'), After('.*')]
public readonly string|null $coeur,
) {
}
}
$bj = new BogJug();
$tw = $bj->one(TinWoodman::class, <<<OZ
One of the big trees had been partly chopped through, and standing beside
it, with an uplifted axe in his hands, was a man made entirely of tin. His head
and arms and legs were jointed upon his body, but he stood perfectly motionless,
as if he could not stir at all.
OZ);
dump($tw);
^ tests\Data\TinWoodman^ {#427
+noggin: "head"
+upperLimbs: "arms"
+lowerLimbs: "legs"
+torso: "body"
+coeur: null
}