Practical Regex 2: Using Named Backreference in Positive Lookahead
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 $Domain
Repository 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 😉
leave a comment