in reply to Re^2: bracket processing
in thread bracket processing
"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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: bracket processing
by AnomalousMonk (Archbishop) on Apr 01, 2020 at 18:23 UTC | |
by kcott (Archbishop) on Apr 02, 2020 at 01:44 UTC | |
by AnomalousMonk (Archbishop) on Apr 02, 2020 at 03:16 UTC |