Documenting Perl Regular Expressions

Regular expressions can easily be documented which is demonstrated in the following example matching floating point numbers:

/^
    [+-]?            # first, match an optional sign
    (                # then match integers or f.p. mantissas:
        \d+.\d+      # mantissa of the form a.b
        |\d+.        # mantissa of the form a.
        |\.\d+       # mantissa of the form .b
        |\d+         # integer of the form a
    )
    ([eE][+-]?\d+)?  # finally, optionally match an exponent
$/x;

See also: Avoiding Regular Expressions

Feedback is always welcome! If you'd like to get in touch with me concerning the contents of this article, please use Twitter.