"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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.