Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Answer: Using CODEconstant/CODEs in regexes?

( #104064=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > Using constants 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,,,

Comment on Answer: Using CODEconstant/CODEs in regexes?
Download Code
Re: Answer: Using CODEconstant/CODEs in regexes?
by japhy (Canon) on Aug 11, 2001 at 03:11 UTC
    Err, I think you were thinking of (??{ ... }). That's overkill, in my opinion. Your approach doesn't work, since (?{ ... }) merely executes code, it doesn't use it as part of the regex.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      You are absolutely right: it was $$ not $. And yes it is an overkill, but it works. I just couldn't remember the syntax of @{[...]}.

      Thanks for the correction

      Aziz,,,

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 avoiding work at the Monastery: (4)
As of 2015-07-05 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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









    Results (60 votes), past polls