TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Regular Expressions in CoffeeScript are Awesome

45 点作者 elijahmanor超过 13 年前

12 条评论

mike-cardwell超过 13 年前
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 未加载
phuff超过 13 年前
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 未加载
jashkenas超过 13 年前
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>
joshuahedlund超过 13 年前
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_downs超过 13 年前
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 未加载
gmac超过 13 年前
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 未加载
samarudge超过 13 年前
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 未加载
jriddycuz超过 13 年前
"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 未加载
SeoxyS超过 13 年前
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 未加载
shtylman超过 13 年前
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 未加载
buddydvd超过 13 年前
What about trailing spaces? They need to be escaped by parenthesis?
mrpollo超过 13 年前
is there any performance penalty?
评论 #3568114 未加载