Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Using constants in regexes?

( #104059=categorized question: print w/ replies, xml ) Need Help??
Contributed by Kozz on Aug 11, 2001 at 02:31 UTC
Q&A  > regular expressions


If I were to create something like
use constant TEST => 'def'; my $string = 'abcdefg'; if ($string =~ /TEST/) { print "The string contains the constant.\n"; }else{ print "The string doesn't contain the constant.\n"; }
It's awfully hard for the interpreter to know that TEST is actually a constant, because to the regex engine it's just another string. The /e modifier doesn't work. Is there a way to do this?

Answer: Using CODEconstant/CODEs in regexes?
contributed by japhy

The /e modifier is for the right-hand side of a s/// only. You might want to just leave out the slashes.

use constant PATTERN => qr/def/; if ($string =~ PATTERN) { ... }
Or, if you need to embed a constant, use the array dereference-reference trick:
if ($string =~ /this@{[ PATTERN ]}that/) { ... }
Answer: Using CODEconstant/CODEs in regexes?
contributed by abstracts

Use the (?{ CODE }) experimental feature at your own risk:

use constant TEST => 'def'; my $string = 'abcdefg'; if ($string =~ /(?{TEST})/) { print "The string contains the constant.\n"; }else{ print "The string doesn't contain the constant.\n"; }


Answer: Using constants in regexes?
contributed by dclayton

There's a few ways of tackling this problem, depending on your goals. To use a constant as an interpolated value within a regular expression, use the following notation:

if ($string =~ /${\(TEST)}/) {
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:
use constant PATTERN => qr/def/; if ($string =~ PATTERN) { ... }
An alternative is to leave the constant in string format, and compile it just prior to executing the regex:
use constant PATTERN => 'def'; my $regex = qr/${\(PATTERN)}/; #options such as /m can go here. if ($string =~ regex) { ... }
This gives you the added benefit of being able to easily print the regex out for debugging purposes.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (12)
    As of 2015-07-28 18:11 GMT
    Find Nodes?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

      Results (258 votes), past polls