views:

173

answers:

3
PCRE: /\A[A-Z0-9_\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[a-z]{2,4}|museum|travel)\z/i 
POSIX: /^[A-Z0-9_\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)$/i

This regex is correct in every way for my needs except that it allows emails such as [email protected]. It says these are a match. If I'm not mistaken, doesn't the {2,4} after [A-Z] mean that it has to be between 2 and 4 characters? Could it be a problem with the altercation and museum and travel? I have verified that these are allowed through in my application, and a few regex testers. Also, I'm fuzzy on whether I did do the PCRE and POSIX correctly. I fear they might both just be PCRE and I'm being ridiculous. AFAIK, JavaScript is POSIX and Ruby is PCRE. So if I didn't get that right, lemme down easy, it has been a long, long night of learning regexes, in and out, and I'm trying to look badass. :)

+10  A: 

It does, but it's only applied to the top-level domain name (the "com" in your example).

David M
Oh jeez. You're absolutely right. Thanks man.
Zach
Just as an aside am I right about PCRE = Ruby and POSIX = Javascript?
Zach
No. Ruby doesn't have Perl Regexps, it has Ruby Regexps. More precisely: Ruby 1.8 has Ruby 1.8 Regexps, and Ruby 1.9 has Ruby 1.9 Regexps, and the two are quite different. (Ruby 1.9 is much more powerful.) Ruby 1.8's Regexps are a totally independent implementation, Ruby 1.9's is based on a heavily modified fork of the Oniguruma Regexp engine. Neither Ruby 1.8 nor Oniguruma are based on anything, except of course that their respective authors know the Friedl book (Mastering Regular Expressions), PCRE, Perl, POSIX BRE and ERE and so on.
Jörg W Mittag
Ah. Thanks very much Jörg, I knew that PCRE was Perl RegExps but I thought it was a standard like POSIX. I had no idea about that book though, have you read it? I'm just really getting started diving into truly understanding what RegExps do. Holiday project. :) Wondering if I need to pick that book up for myself?
Zach
Friedl's book is excellent. If you want to get into the nuts and bolts of regexes, it's the book for you. For a more practical approach, check out http://www.regular-expressions.info or the "Regular Expressions Cookbook" by Jan Goyvaerts/Steve Levithan.
Tim Pietzcker
Javascript regular expressions are based on Perl, not POSIX. I don't know if it's exactly the same as PCRE, but it's similar at least.
Matthew Crumley
A: 

POSIX does not support non-capturing groups (?:group) so neither of your regexes are POSIX. JavaScript does not use POSIX. JavaScript regular expressions are Perl-style but it doesn't have all the features. Check the regex flavor comparison on my site for details.

Jan Goyvaerts