Welcome to Abdul Malik Ikhsan's Blog

Practical Regex 2: Using Named Backreference in Positive Lookahead

Posted in regex, tips and tricks by samsonasik on September 15, 2021

When we have the following namespaced classname value:

App\Domain\Foo\FooRepository

and our task is to get “Foo” value as Domain group with the criteria:

  • must have App\Domain\ prefix
  • must have \$Domain name + “Repository” suffix which $Domain must match previous sub-namespace, on this case, “Foo”

we can use the following regex with Positive Lookbehind to filter prefix and Positive Lookahead to filter suffix:

(?<=App\\Domain\\)(?<Domain>[A-Z][a-z]{1,})(?=\\\1Repository)

Above, we find $Domain with App\Domain\ before it with Positive Lookbehind, and $DomainRepository after it with Positive Lookahead.

We are using backreference with \1 to match the exact same text of the first capturing group. If you code in PHP, you can do like this:

$pattern   = '#(?<=App\\\\Domain\\\\)(?<Domain>[A-Z][a-z]{1,})(?=\\\\\1Repository)#';
$className = 'App\Domain\Foo\FooRepository';

preg_match($pattern, $className, $matches);
if ($matches !== []) {
    echo $matches['Domain'];
}

What if the code is getting more complex, like in my previous post for named capturing group, you need to remember the numbered index! To handle it, you can use named backreference for it, so the regex will be:

(?<=App\\Domain\\)(?<Domain>[A-Z][a-z]{1,})(?=\\\k<Domain>Repository)

Now, you are exactly know what backrefence reference to. If you code in PHP, you can do like this:

$pattern   = '#(?<=App\\\\Domain\\\\)(?<Domain>[A-Z][a-z]{1,})(?=\\\\\k<Domain>Repository)#';
$className = 'App\Domain\Foo\FooRepository';

preg_match($pattern, $className, $matches);
if ($matches !== []) {
    echo $matches['Domain'];
}

That’s it 😉

Ref: https://www.regular-expressions.info/backref.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: