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

I have some test code that gets a page with Test::WWW::Mechanize and tries to test its content. The content I'm looking for looks like this:
<tr class="meld"> <th>Security Code Matched</th> <td>N/A</td> </tr>
This is my test code. I had to use two steps to extract the row I want and then check it.
my ($security_code_row) = ($mech->content =~ m{Security Code Matched(.*?)</tr>}s); like($security_code_row, qr{N/A}, 'code matched is N/A');
Caveats about parsing HTML with regexes aside, can anyone think of a way to do this in one regex (i.e. with $mech->content_like)? It's mostly an academic question, since what I have now is working fine.

Replies are listed 'Best First'.
Re: combine two-part regex test
by Zaxo (Archbishop) on Jan 29, 2007 at 21:07 UTC

    HTML parsing aside, I'd prefer two regexen to one containing the two pasted together somehow.

    The computational complexity of regexen increases dramatically with length. That makes two smaller and simpler ones better in my book.

    Since what you are looking for seems to be literals, you may want to think of index instead of a regex.

    After Compline,
    Zaxo

Re: combine two-part regex test
by AltBlue (Chaplain) on Jan 30, 2007 at 00:24 UTC
    fancy experimental features? ;-)
    #!/usr/bin/perl -l use strict; use warnings; my @str = split /\n/, <<'HTML'; <tr class="meld"><th>Security Code Matched</th><td>foo</td></tr> <tr><th>Security Code Matched</th><td>N/A</td></tr> <tr><th class="foo">Security Code Matched</th><td>N/A</td></tr> <tr><th>Security Code Matched</th><td>10!</td></tr> <tr><td>Security Code Matched</td><td class="val">N/A</td></tr> HTML for (@str) { if ( m{ \bSecurity\ Code\ Matched\b (.*?)</tr> (?(?{ index($^N, 'N/A') < 0 })!) }xsg ) { print "[[$_]]"; } }
    output:
    [[<tr><th>Security Code Matched</th><td>N/A</td></tr>]] [[<tr><th class="foo">Security Code Matched</th><td>N/A</td></tr>]] [[<tr><td>Security Code Matched</td><td class="val">N/A</td></tr>]]
    i wouldn't use it at home, though ;-)
Re: combine two-part regex test
by BrowserUk (Patriarch) on Jan 29, 2007 at 21:11 UTC
      The problem is that if a different row in the table contains "N/A", it will still match.