http://www.perlmonks.org?node_id=481466

This is the second post in the How To Ask a Question (With Test::More) series, and assumes you have read the first, main post.

In this installment, we learn how to use Test::More::like($string, $regex) and the qr quotelike operator to ask regex questions in a sane way. Let's dive right in.

******************BEGIN QUESTION TEMPLATE************************

O Wise Monks, I can't get this regex to do what I want it to do. As you can see, it fails two tests.

use warnings; use strict; use Test::More qw(no_plan); my $emailsregex = qr/\w+@\w+\.(net|com|org)/; while (<DATA>) { like($_, $emailsregex) } __DATA__ timmytimmers@aol.com hillary-hillers@aol.com garbygerbil@herbill.gerbil.org garbygerbil@herbill.gorgil.org reginald_reggers@aol.com
I'm on Windows XP system using the standard ActiveState perl distribution.

I got both standard and error output out of my test, into one output file, by running this with emailsregex.pl > output.txt 2>&1 output.txt.

The output I got was

ok 1 ok 2 not ok 3 # Failed test (emailsregex.pl at line 8) # 'garbygerbil@herbill.gerbil.org # ' # doesn't match '(?-xism:\w+@\w+\.(net|com|org))' not ok 4 # Failed test (emailsregex.pl at line 8) # 'garbygerbil@herbill.gorgil.org # ' # doesn't match '(?-xism:\w+@\w+\.(net|com|org))' ok 5 1..5 # Looks like you failed 2 tests of 5.

What the heck am I doing wrong?

******************END QUESTION TEMPLATE************************

(Debugging the regex is left as an exercise for the reader.)

Replies are listed 'Best First'.
Re: How to Ask a Regex Question (with Test::More) (repost, with Sitedocclan approval)
by kvale (Monsignor) on Aug 06, 2005 at 14:31 UTC
    I'm a fan of test-driven development and I like the template you have created. Something like it could be useful for anyone with experience programming perl.

    But a regex matching problem may be part of the first perl program a Seeker has ever written. Asking the Seeker to understand DATA statements and modules (where does like() come from? how is it defined?) may be raising the bar too high for a new user.

    Perlmonks welcomes novices :)

    -Mark

Re: How to Ask a Regex Question (with Test::More) (repost, with Sitedocclan approval)
by Eimi Metamorphoumai (Deacon) on Aug 08, 2005 at 15:39 UTC
    My problem with this template is that what regex is needed is usually defined as much by what strings it should not match as by what it should. Also, when it does match, how. So it would be very nice if the test harness let you say "the string 'timmytimmers@aol.com' should match with $1 eq 'timmytimmers' and $2 eq 'aol.com' but the string 'herbill.gerbil.org!garygerbil' shouldn't match at all". Otherwise, the correct answer is always /.*/.

      I assumed that Test::More would have this in the bag, but went ahead and checked. Test::More certainly handles this already with an unlinke($this, $regex). This should probably be mentioned in the template (which I'm sure is what you meant.) As for matching HOW it matched, that would be an interesting problem. Perhaps a the test harness should accept an arrayref where the first element is the regex and the following should be $1..$9 in order?

      like("Hello World", [ /(.+)\s(.+)/, "Hello", "World"]);

      Seems like a mouth full but it would handler your case.

      Just my two cents.


      ___________
      Eric Hodges
Re: How to Ask a Regex Question (with Test::More) (repost, with Sitedocclan approval)
by chanio (Priest) on Aug 07, 2005 at 00:30 UTC
    It could be useful if a CGI script somewhere here, could copy the original failing script and rebuild it to use it this way... With the copy & paste option for doing the questions.

    Some fields should be filled by the novice, though.

    { \ ( ' v ' ) / }
    ( \ _ / ) _ _ _ _ ` ( ) ' _ _ _ _
    ( = ( ^ Y ^ ) = ( _ _ ^ ^ ^ ^
    _ _ _ _ \ _ ( m _ _ _ m ) _ _ _ _ _ _ _ _ _ ) c h i a n o , a l b e r t o
    Wherever I lay my KNOPPIX disk, a new FREE LINUX nation could be established