Jump to content
  • Advertisement
Sign in to follow this  
prh99

Perl 5 regular expressions[Answered]

This topic is 4644 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am in the process of learning Perl. Is there anyway to store a regular expression in a variable. For example, the expression /\d{3}-\d{3}-\d{4}, is there anyway to store it in a variable called say phone_number and use the variable name in place of the actual expression in pattern matching etc. I've searched Google without much luck. Of course it is possible I've over looked something. Any help would be much appreciated. Thanks. [Edited by - prh99 on March 31, 2006 1:22:08 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Sharlin
Well, it's straightforward:
$variable = /\d{3}-\d{3}-\d{4}/;



but when you attempt to use the variable in pattern matching the result
will be true no matter what it's applied to.



Thanks for the help, both of you. :)

[Edited by - prh99 on March 30, 2006 2:04:16 PM]

Share this post


Link to post
Share on other sites
Yep, qr{...} will do the trick and has some speed advantages where it is used standalone. /.../ or m{...} will interpolate (m'...' being the exception) which means you can stuff all sorts of values into your regular expressions just as you do for "..." or qq{...}.


#!/usr/bin/perl
use strict;
use warnings;

my $thing = '(?:\d{3}-){2}\d{4}';
print 'match' if '012-345-6789' =~ m/$thing/;


See 'perldoc perlop' and 'perldoc perlre' for the gory details :)

Share this post


Link to post
Share on other sites
Thanks very much, qr{} is what I was looking for. Also thanks for the Perl resources.

rating++

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!