Live data from Hacker News

Regular Expressions in CoffeeScript are Awesome

elijahmanor.com

11–20 of 28 posts

Re: Regular Expressions in CoffeeScript are Awesome

#11
His example, slightly modified for Perl:

  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";
  }
EDIT: A much better way of doing it though:

  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";
  }

Re: Regular Expressions in CoffeeScript are Awesome

#12

His example, slightly modified for Perl: 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 { prin…

Nice! I like it ;)

Re: Regular Expressions in CoffeeScript are Awesome

#13
post #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-...

Yeah, my guess is that CoffeeScript took direction from those languages for this feature.

Nice function to convert annotated string to a regex. I wonder if this is being considered in the next versions of JavaScript? Or better yet... native annotation?

I like the CoffeeScript approach since that logic is done at compile time and not when it is being executed.

Re: Regular Expressions in CoffeeScript are Awesome

#14
post #10

Earlier quoted context omitted.

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 (exc…

Vim and awk use the old syntax, and that irritates me to no end.

Re: Regular Expressions in CoffeeScript are Awesome

#16

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.

The example was to show how you could do it, not necessarily that it was needed for that case. However, you might be surprised how many developers aren't familiar with that example regex.

Re: Regular Expressions in CoffeeScript are Awesome

#17
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...

https://github.com/jashkenas/coffee-script/blob/master/src/l...

Re: Regular Expressions in CoffeeScript are Awesome

#18

His example, slightly modified for Perl: 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 { prin…

And for just email address checking then even better IMHO is:

  use Email::Valid;

  if (Email::Valid->address('john.smith@gmail.com')) {
    print "E-mail is valid\n";
  }
  else { print "Email is invalid\n" }
ref: https://metacpan.org/module/Email::Valid

Re: Regular Expressions in CoffeeScript are Awesome

#19
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.
Post reply on HN