I think you're asking for
/ ^ ( (?: (?!\Q$key\E). )* ) ( .* ) /xs
By the way, you shouldn't be using \1 and \2 in the substitution expression (as Perl told you through warnings you apparently ignored). \1 and \2 are regex atoms that instruct the regex engine to match first and second captured string respectively. It makes no sense to use them in the substitution expression. Use $1 and $2 instead.
In reply to Re: Capturing groups where the ending is optional
by ikegami
in thread Capturing groups where the ending is optional
by cwm9
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |