Skip to content

Instantly share code, notes, and snippets.

@smonff
Created January 17, 2014 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smonff/8467532 to your computer and use it in GitHub Desktop.
Save smonff/8467532 to your computer and use it in GitHub Desktop.
An excerpt from chromatic's Modern Perl book about clean regexes
# The /x modifier allows you to embed additional whitespace and comments within patterns.
# With this modifier in effect, the regex engine ignores whitespace and comments.
# The results are often much more readable.
# This regex isn't simple, but comments and whitespace improve its readability.
# Even if you compose regexes together from compiled fragments, the /x modifier
# can still improve your code.
my $attr_re = qr{
\A # start of line
(?:
[;\n\s]* # spaces and semicolons
(?:/\*.*?\*/)? # C comments
)*
ATTR
\s+
( U?INTVAL
| FLOATVAL
| STRING\s+\*
)
}x;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment