Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

How to Ask a Regex Question (with Test::More) (repost, with Sitedocclan approval)

by tphyahoo (Vicar)
on Aug 06, 2005 at 10:34 UTC ( #481466=perltutorial: print w/replies, xml ) Need Help??

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__
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 > output.txt 2>&1 output.txt.

The output I got was

ok 1 ok 2 not ok 3 # Failed test ( at line 8) # ' # ' # doesn't match '(?-xism:\w+@\w+\.(net|com|org))' not ok 4 # Failed test ( at line 8) # ' # ' # 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 :)


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 '' should match with $1 eq 'timmytimmers' and $2 eq '' but the string '!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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://481466]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-12-07 03:09 GMT
Find Nodes?
    Voting Booth?

    No recent polls found