I'm writing a Perl regex to match both the strings x bla
and [x] bla
. One alternative is /(?:x|\[x\]) bla/
. This isn't desirable, because in the real world, x
is more complicated, so I want to avoid repeating it.
The best solution so far is putting x
in a variable and pre-compiling the regex:
my $x = 'x';
my $re = qr/(?:$x|\[$x\]) bla/o;
Is there a neater solution? In this case, readability is more important than performance.