What are best practices for using assertions in PHP regex to avoid errors like "NOTHING TO REPEAT AT OFFSET"?

When using assertions in PHP regex, it's important to ensure that assertions are not placed in positions where they can cause the "NOTHING TO REPEAT AT OFFSET" error. This error occurs when a quantifier (such as *, +, or {n,m}) is placed directly after an assertion, causing the assertion to potentially match nothing and resulting in the error. To avoid this issue, make sure to place assertions within the regex pattern in a way that ensures they are always preceded by a character or group that can be quantified.

// Example of using assertions in PHP regex without causing "NOTHING TO REPEAT AT OFFSET" error
$pattern = '/(?<=prefix)pattern(?=suffix)/';
$string = 'prefixpatternsuffix';

if (preg_match($pattern, $string, $matches)) {
    echo "Match found: " . $matches[0];
} else {
    echo "No match found.";
}