Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Anyway I can put the the data matching in a variable??
$data =~ /(?:match|matching\.com|match\.company)/gi)
I tried this and it didnt work:
my $first = 'match'; my $second = 'matching\.com'; my $third = 'match\.company'; $data =~ /(?:$first|$second|$third)/gi)

Replies are listed 'Best First'.
Re: Variable in pattern matching line
by broquaint (Abbot) on Jul 01, 2003 at 13:04 UTC
    Anyway I can put the the data matching in a variable??
    You can use the qr// construct to build regular expression objects e.g
    my $re = qr/(?:match|matching\.com|match.company)/i; print "regexp - $re"; __output__ regexp - (?i-xsm:(?:match|matching\.com|match.company))
    You might also want to consider Regex::PreSuf. Another thing to note is that due to the nature of alternation in perl's regex engine anything with 'match' will always match (regardless of the 'ing.com' or '.company'), making the other two alternations redundant. So you'll want to reverse the order of the alternation groups e.g
    my $re = qr/(match|matching\.com|match.company)/i; print "old - $1\n" if "matching.com" =~ $re; $re = qr/(matching\.com|match.company|match)/i; print "new - $1\n" if "matching.com" =~ $re; __output__ old - match new - matching.com
    See. perlre for more info on regexes in perl.
    HTH

    _________
    broquaint

Re: Variable in pattern matching line
by Tomte (Priest) on Jul 01, 2003 at 13:05 UTC

    maybe you copy'ed and pasted sloppy, but it works if I remove that extra paren:

    #!/usr/bin/env perl my $first = 'match'; my $second = 'matching\.com'; my $third = 'match\.company'; my $data = 'matching.com'; my $test = $data =~ /(?:$first|$second|$third)/gi; print $test , "\n"; __END__ 1
    be sure to check if $data is actualy a matching string, just print it to STDERR or something. Also it's a nice habit to post the occuring errors and sample data, helps a lot to help :)

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      Thanks to both of you for helping me solve this problem.
Re: Variable in pattern matching line
by mhearse (Chaplain) on Jul 01, 2003 at 16:45 UTC
    There is an easy way to "capture" your pattern matches. This can be done by grouping your expression with parens (). The mathces made will then be in $1, $2, $3 ... Such as:

    $_ = "FOO:BAR";
    /(.*?):(.*)/;

    No you will have $1 = FOO and $2 = BAR
Re: Variable in pattern matching line
by dreadpiratepeter (Priest) on Jul 01, 2003 at 13:45 UTC
    Be careful, you are not doing what you think you are doing. The backslash in your string is being interpreted as an escape char.
    You want $second = 'matching\\.com'. Otherwise you are generating a match on "matching.com" which will match "matchingfcom", etc.

    UPDATE: Disregard my inane comments. See below. I'm a moron. The big yellow one's the sun.

    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

      No, the '\' isn't interpreted in single quotes.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

        duh, my mistake. I wrote a little test to verify that and accidently typed double quotes then didn't notice it. That's what I get for trying to answer questions while in the middle of something at work.

        -pete
        "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."