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

Hi, I am not able to understand the following reqular expression. Any help me on this would be appreciated. regex => qr/^ (?: \d+ [hmD] ) $/x

Replies are listed 'Best First'.
Re: Regular expression
by CountZero (Bishop) on Mar 03, 2012 at 10:23 UTC
      (?x:                     group, but do not capture (disregarding
                               whitespace and comments) (case-sensitive)
                               (with ^ and $ matching normally) (with .
                               not matching \n):
    ----------------------------------------------------------------------
        ^                        the beginning of the string
    ----------------------------------------------------------------------
        (?:                      group, but do not capture:
    ----------------------------------------------------------------------
          \d+                      digits (0-9) (1 or more times
                                   (matching the most amount possible))
    ----------------------------------------------------------------------
          hmD                    any character of: 'h', 'm', 'D'
    ----------------------------------------------------------------------
        )                        end of grouping
    ----------------------------------------------------------------------
        $                        before an optional \n, and the end of
                                 the string
    ----------------------------------------------------------------------
      )                        end of grouping
    Thanks to YAPE::Regex::Explain

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Thank you CountZero :)
Re: Regular expression
by tobyink (Canon) on Mar 03, 2012 at 10:14 UTC

    Matches a string that starts with one or more digits, followed by an "h", "m" or "D" (case sensitive), and then ends.

    The (?: ... ) structure in this case doesn't do anything. It might as well have been written:

    qr/^\d+[hmD]$/
      Thank u :)
Re: Regular expression
by toolic (Bishop) on Mar 03, 2012 at 13:29 UTC
Re: Regular expression
by Anonymous Monk on Mar 03, 2012 at 10:11 UTC