Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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


Description:

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"; }

Aziz,,,

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.
Answer: Using constants in regexes?
contributed by freonpsandoz

I'm not sure using qr is the best advice. This fuses what the constant *represents* with just one potential use of that constant, which will lead to problems maintaining the code, because using that constant for anything else (like print or eq) will give incorrect results. Most of these incorrect uses don't even give a compile error; they just produce incorrect results.

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!
  • 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, 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (12)
    As of 2016-02-09 16:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?





      Results (320 votes), past polls