in reply to Using constants in regexes?
This has the unfortunate consequence of slowing down your regular expression, since it can't be compiled. Using the qr operator is your best bet. You can use japhy's suggested format:if ($string =~ /${\(TEST)}/) {
An alternative is to leave the constant in string format, and compile it just prior to executing the regex:use constant PATTERN => qr/def/; if ($string =~ PATTERN) { ... }
This gives you the added benefit of being able to easily print the regex out for debugging purposes.use constant PATTERN => 'def'; my $regex = qr/${\(PATTERN)}/; #options such as /m can go here. if ($string =~ regex) { ... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Answer: Using constants in regexes?
by diotalevi (Canon) on Feb 01, 2005 at 20:48 UTC |