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

@answertext = ("big","huge","large"); $regexpans = join('|',@answertext); $regexpans = '/^('.$regexpans.')\b/';
the $regexpans is then checked verses another value to see if they are the same
what does the regex do?

Replies are listed 'Best First'.
Re: what does this regex do?
by fishbot_v2 (Chaplain) on Nov 07, 2005 at 21:57 UTC

    The join of the array leaves $regexpans containing:

    /^(big|huge|large)\b/

    It's just a 'generated regex'. I'm not sure why it wasn't written:

    $regexpans = qr/^($regexpans)\b/;
Re: what does this regex do?
by GrandFather (Saint) on Nov 07, 2005 at 22:03 UTC

    What have you tried? Have you for example even tried printing the value in regexpans at then end of the process (assuming you can't figure out what it will contain)? Have you consulted any of the plethora of regex documentation in perldocs (perlre, perlretut ...)?

    Note that PerlMonks is not a "Do your homework" service. If you show effort we will help, but show some effort first.


    Perl is Huffman encoded by design.
Re: what does this regex do?
by muba (Priest) on Nov 07, 2005 at 22:03 UTC
    What this will do is create a variable, $regexpans with a value of /^(big|huge|large)\b.

    This is not a regexp, it is a string. However, would you be going to use this as a regexp somehow (in a eval statement probably, I don't know), the regexp would do the following:

    • Match any of the following
      • big
      • huge
      • large
    • ... but only if both of the following conditions are true
      • it is at the beginning of the string
      • it is followed by a word-boundary, be this a tab, a space, a newline, a end-of-string or anything else that is part of the definition of \b
      It won't match anything, because the '/' is part of the pattern, and is followed by the beginning-of-string anchor.

      Caution: Contents may have been coded under pressure.
        Quote from self: in a eval statement probably, I don't know

        eval " [...] s${regexpand}foobar/; [...] ";
        This code wil validly substitute any of those three words at the beginning of a string and before a word boundary with 'foobar'. I see no reason for doing this but it is perfectly valid.
        Unless I'm completely wrong of course :p