"As a general practice, I find it's much safer to interpolate strings ... into regexes using \Q \E ..."
As a general rule, for regexes in general, that's fine and I'd generally do the same; however, bracketed classes are different.
Take a look at "perlrecharclass: Special Characters Inside a Bracketed Character Class". I'll leave you to acquaint yourself with the full text. Here's some pertinent extracts (my emphasis added):
Most characters that are meta characters in regular expressions ... lose their special meaning and can be used inside a character class without the need to escape them.
...
Characters that may carry a special meaning inside a character class are: \ , ^ , - , [ and ] , and are discussed below.
...
A [ is not special inside a character class, unless it's the start of a POSIX character class ... It normally does not need escaping.
So, none of the characters in $delim required escaping.
Furthermore, I generally aim to thoroughly test my solutions before posting them. In this instance, I had added a temporary print statement:
my $prefix = qr{[^$delim]*}; print "$prefix\n";
which output:
(?^:[^([{<]*)
That's exactly the regex I wanted.
— Ken
In reply to Re^3: bracket processing
by kcott
in thread bracket processing
by rajaman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |