TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Regular Expressions in CoffeeScript are Awesome

45 pointsby elijahmanorover 13 years ago

12 comments

mike-cardwellover 13 years ago
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>
评论 #3567093 未加载
评论 #3566724 未加载
phuffover 13 years ago
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>
评论 #3566636 未加载
jashkenasover 13 years ago
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>
joshuahedlundover 13 years ago
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.
peter_l_downsover 13 years ago
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>
评论 #3566651 未加载
gmacover 13 years ago
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>
评论 #3566767 未加载
samarudgeover 13 years ago
You shouldn't validate emails by regular expressions since, technically (at least according to spec)<p>"()&#60;&#62;[]:;@,\\\"!#$%&#38;'*+-/=?^_`{}| ~ ? ^_`{}|~."@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>
评论 #3566622 未加载
评论 #3568669 未加载
jriddycuzover 13 years ago
"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.
评论 #3568759 未加载
SeoxySover 13 years ago
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.
评论 #3567557 未加载
shtylmanover 13 years ago
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.
评论 #3566819 未加载
buddydvdover 13 years ago
What about trailing spaces? They need to be escaped by parenthesis?
mrpolloover 13 years ago
is there any performance penalty?
评论 #3568114 未加载