in reply to Re: Regexp question
in thread Regexp question

One last quick question. Is this valid for checking whether the match occurred, and would $1 be initialized to the matched text in this code?
if ( $html =~ m/id="$element" name="challenge" value="(.*?)"/ ) { $post{$element} = $1; }

use strict; use CGI;

Replies are listed 'Best First'.
Re^3: Regexp question
by toolic (Bishop) on Dec 20, 2007 at 17:42 UTC
    I believe so, but you can easily prove it for yourself with a trivial testcase.
Re^3: Regexp question
by FunkyMonk (Bishop) on Dec 21, 2007 at 00:18 UTC
    Yes, that'll work. As will
    $post{$element} = $1 if $html =~ m/id="$element" name="challenge" value="(.*?)"/;

    Unless you expect $element to contain metacharacters, you should use

    m/id="\Q$element\E" name="challenge" value="(.*?)"/;

    to escape any metacharacters in $html. See quotemeta for more.