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

  1. I want to spread my regexp described using qr over multiple lines.
  2. Also I remember that there was some syntax for putting modifiers into a precompiled string using ? but forgot where I saw it. I want to add i and x to the regexp below.
  3. Does anyone know why this regexp appears to be matching but I am not getting any output in $1 $2 $3, etc?

    package Regexp; use strict; use vars qw($file_re); my $underscore = '_'; my $non_dot = '[^.]'; my $dot = '[.]'; $file_re = qr/((nonconsolidated|other)$underscore)?(faq|organization|literature +|contact|service)$underscore($non_dot+)${dot}xls/ ; 1; use Regexp; use strict; my @file = qw( Organization_DOT.xls.txt other_Service_FDNY.xls.txt nonconsolidated_FAQ_NYPD.xls.txt ); for (@file) { warn $_; if (/$Regexp::file_re/i) { warn "$1 $2 $3 $4"; warn $&; } else { warn 'AaaaaaaH'; } }

update (broquaint): added ending </ol> tag

Replies are listed 'Best First'.
Re: [perlre] using qr - help needed
by gjb (Vicar) on Nov 07, 2002 at 21:55 UTC

    I think you want

    $file_re = qr/(?:(nonconsolidated|other)$underscore)? (faq|organization|literature|contact|service) $underscore($non_dot+) ${dot}xls/ix;
    to (1) not capture the first brackets and (2) to get case insensitivity (should be regexp compile time and (3) that's where the x to ignore whitespace should be as well.

    Hope this helps, -gjb-

    Update: added formatting and x option to qr.

Re: [perlre] using qr - help needed
by Aristotle (Chancellor) on Nov 08, 2002 at 01:43 UTC
    The syntax your are looking for is (?ix)$Regexp::file_re or (?ix:$Regexp::file_re), as detailed in perlre - but go with what gjb said.

    Makeshifts last the longest.

Re: [perlre] using qr - help needed
by broquaint (Abbot) on Nov 08, 2002 at 00:03 UTC
    This looks a little neater IMHO
    package Regexp; use strict; use vars qw($file_re); my $underscore = qr/_/; my $non_dot = qr/[^.]/; my $dot = qr/[.]/; $file_re = qr< # this is now case insensitive and non-capturing (?i-xsm: ( nonconsolidated | other ) $underscore )? ( faq | organization | literature | contact | service ) $underscore ($non_dot+) ${dot}xls >x; q</package Regexp>;
    But then again I do have a thing for indented regexp :)
    HTH

    _________
    broquaint

Re: [perlre] using qr - help needed
by princepawn (Parson) on Nov 08, 2002 at 14:57 UTC
    Why aren't you guys using the /o modifier?
      Because qr compiles the regex it makes the /o modifier rather superfluous. It does however apply to situations where you have a match which has a variable in it and you only want it to be compiled once e.g
      my $match = shift @ARGV; my @matched = grep /^$match$/o, <>;
      So there the regex will only be compiled once instead for each iteration within the grep. See the perlop|perlop manpage for more info.
      HTH

      _________
      broquaint

        That can be done with qr// as well though.
        my $rx = qr/^@{[shift @ARGV]}$/; my @match = grep /$rx/, <>;
        qr// also is much less likely to be used mistakenly than /o and much easier to spot when it is. /o should be considered a relic from pre-qr// times that has few if any uses anymore.

        Makeshifts last the longest.