Live data from Hacker News

Regular Expressions in CoffeeScript are Awesome

elijahmanor.com

1–10 of 28 posts

Re: Regular Expressions in CoffeeScript are Awesome

#2
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).

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:

http://blog.mackerron.com/2010/08/08/extended-multi-line-js-...

Re: Regular Expressions in CoffeeScript are Awesome

#5

You shouldn't validate emails by regular expressions since, technically (at least according to spec) "() []:;@,\\\"!#$%&'*+-/=?^_`{}| ~ ? ^_`{}|~."@example.org is a perfectly valid email address [1] [1] http://en.wikipedia.org/wiki/Email_address#Valid_email_addre...

More like you shouldn't validate emails by using crappy regular expressions?

AFAIK there are regular expressions that cover all valid email addresses (and some invalid, but better than not recognizing one that is valid).

Although it gets pretty nasty: http://www.diablotin.com/librairie/autres/mre/chBB.html

Re: Regular Expressions in CoffeeScript are Awesome

#7
post #6

In JS you can comment regular expressions, too... :) var foo = new RegExp( '\\d\+' + // First digit '-' + // Delimiter '\\d\+' // Second digit ); foo.test("1234-5432");

True, but then you have the overhead of string concatenation... although in the large scheme of things that is probably very small, unless you are doing it in a huge loop, but then you'd cache that RegExp anyway.

Re: Regular Expressions in CoffeeScript are Awesome

#8

You shouldn't validate emails by regular expressions since, technically (at least according to spec) "() []:;@,\\\"!#$%&'*+-/=?^_`{}| ~ ? ^_`{}|~."@example.org is a perfectly valid email address [1] [1] http://en.wikipedia.org/wiki/Email_address#Valid_email_addre...

More like you shouldn't validate emails by using crappy regular expressions? AFAIK there are regular expressions that cover all valid email addresses (and some invalid, but better than not recognizing one that is valid). Although it gets pretty nasty: http://www.diablotin.com/librairie/autres/mre/chBB.html

But since nearly every character is valid in an email address before the @ sign (within ""), it's much easier to validate `*@thisisavaliddomain.com` and just try sending an email. The way we do it, if the user enters an email that matches our reasonably comprehensive regexp we consider it valid, if it doesn't match we send the user an email and get them to verify it with a link, that way most users don't have to go through an annoying click-and-confirm email validation but we can still catch a few edge cases of people mistyping

Re: Regular Expressions in CoffeeScript are Awesome

#9

Python, too: http://docs.python.org/library/re.html#re.VERBOSE a = re.compile(r"""\d + # the integral part \. # the decimal point \d * # some fractional digits""", re.X) b = re.compile(r"\d+\.\d*")

Ohh nice, maybe there is where CoffeeScript got inspiration from? The syntax looks very similar.

Re: Regular Expressions in CoffeeScript are Awesome

#10

Python, too: http://docs.python.org/library/re.html#re.VERBOSE a = re.compile(r"""\d + # the integral part \. # the decimal point \d * # some fractional digits""", re.X) b = re.compile(r"\d+\.\d*")

Ohh nice, maybe there is where CoffeeScript got inspiration from? The syntax looks very similar.

Afaik Python, like most open source languages, use the PCRE lib, Perl Compatible Regular Expressions. Google it.

PCRE isn't as good as Perl's regexps, but not bad. (I've helped people with PCRE problems that worked in Perl.)

Edit: I googled myself. :-) The Python lib isn't pcre (anymore), it is something home rolled. The answer to the parent question about inspiration is: Most everyone has copied the Perl syntax (except, irritatingly, Emacs).

Edit 2: herge -- Perl is ~ a superset of awk. Use that. (Check -n and -e flags in perldoc perlrun.)

Post reply on HN