His example, slightly modified for Perl:<p><pre><code> my $emailPattern = qr{ ^ #begin of line
([a-z0-9_.-]+) #one or more letters, numbers, _ . or -
@ #followed by an @ sign
([\da-z.-]+) #then one or more letters, numbers, _ . or -
\. #followed by a period
([a-z.]{2,6}) #followed by 2 to 6 letters or periods
$ }xi; #end of line and ignore case
if( 'john.smith@gmail.com' =~ $emailPattern ){
print "E-mail is valid\n";
} else {
print "E-mail is invalid\n";
}
</code></pre>
EDIT: A much better way of doing it though:<p><pre><code> use Mail::Sendmail;
if( 'john.smith@gmail.com' =~ /$Mail::Sendmail::address_rx/ ){
print "E-mail is valid\n";
} else {
print "E-mail is invalid\n";
}</code></pre>
In JS you can comment regular expressions, too... :)<p><pre><code> var foo = new RegExp(
'\\d\+' + // First digit
'-' + // Delimiter
'\\d\+' // Second digit
);
foo.test("1234-5432");</code></pre>
For a little dollop of recursivity in your morning coffee, here's the bit where the CoffeeScript compiler uses these extended regular expressions to lex, among other things, extended regular expressions...<p><a href="https://github.com/jashkenas/coffee-script/blob/master/src/lexer.coffee#L595-647" rel="nofollow">https://github.com/jashkenas/coffee-script/blob/master/src/l...</a>
I know the email validation was just an example, but since we're on the subject, if you've got an email validation that includes a maximum number of characters for the TLD you might want to update that before the new TLDs start rolling in later this year or next.
Python, too:<p><a href="http://docs.python.org/library/re.html#re.VERBOSE" rel="nofollow">http://docs.python.org/library/re.html#re.VERBOSE</a><p><pre><code> a = re.compile(r"""\d + # the integral part
\. # the decimal point
\d * # some fractional digits""", re.X)
b = re.compile(r"\d+\.\d*")</code></pre>
Perl, Ruby and some other languages support a similar 'extended' RegEx syntax which allows comments and disregards whitespace (in Ruby, you use the 'x' flag on a Regexp literal).<p>For those using plain JavaScript -- or wanting to do a one-off conversion -- I wrote a simple JS function that converts an extended RegEx to a plain one:<p><a href="http://blog.mackerron.com/2010/08/08/extended-multi-line-js-regexps/" rel="nofollow">http://blog.mackerron.com/2010/08/08/extended-multi-line-js-...</a>
You shouldn't validate emails by regular expressions since, technically (at least according to spec)<p>"()<>[]:;@,\\\"!#$%&'*+-/=?^_`{}| ~ ? ^_`{}|~."@example.org<p>is a perfectly valid email address [1]<p>[1] <a href="http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses" rel="nofollow">http://en.wikipedia.org/wiki/Email_address#Valid_email_addre...</a>
"Let's face it, regular expressions aren't for everyone."<p>Wait, since when aren't they? I would have thought basic regex skills were a baseline shibboleth of a programmer. I even know several non-technical people that are proficient.
That has nothing to do CoffeeScript, most regex parser implementation support this via a flag. I've seen it done in PHP, Ruby, Perl, Objective-C. For languages whose regular expression engine don't support this, it's easy enough to achieve it via string concatenation.
I personally don't find this example very helpful in showing why I would do this. The regex is simple enough and to me the comments are the equivalent of "return here", needless and distracting.