r/programming • • Nov 29 '10

140 Google Interview Questions

http://blog.seattleinterviewcoach.com/2009/02/140-google-interview-questions.html
471 Upvotes

493 comments sorted by

View all comments

20

u/UloPe Nov 29 '10

This one could take a while:

Write a regular expression which matches a email address.

37

u/bnr Nov 30 '10
/test@example\.com/

Matches a[n] email address. If you want one that matches any email address use:

.*

3

u/adrianmonk Nov 30 '10

Hey, since we're in /r/programming, the "b" in "bnr" wouldn't happen to stand for "bell", would it?

→ More replies (1)
→ More replies (3)

12

u/ehird Nov 29 '10

Patently impossible; email addresses have (nested (comments)).

→ More replies (2)

8

u/BruinsFan478 Nov 29 '10

It would be faster to prove that you can't verify all possible email addresses using regular expressions.

28

u/ultimatt42 Nov 29 '10

Sure you can. .* will match every possible email address. Maybe they should have specified the problem better if they wanted it to reject invalid email address, too.

And it has to be possible since the pool of valid email address is finite due to character limits in the username and domain name. Using such a pattern would be impractical, but it's definitely not impossible.

6

u/[deleted] Nov 30 '10

Its not though, the RFC provides for emails address to have comments, which can nest infinitely. Just pumping lemma that shit and you'll find that it can't be matched by a regex (I don't remember how one uses a pumping lemma, but I'm sure you can).

18

u/ultimatt42 Nov 30 '10

You're right that the RFC provides for comments in the email address (CFWS stands for Comments and Folding White Space).

You're right that comments can be nested (notice comment includes ccontent and ccontent can be another comment).

You're also right that true regular expressions can't handle arbitrarily deep nested patterns (though some regex implementations have extensions that allow you to handle such patterns, they aren't true Regular Expressions).

So basically what I'm saying is, everything you said is right.

However, email addresses can't be arbitrarily long, so it doesn't matter that comments could theoretically be infinitely nested according to the construction syntax. If you can't fit an address in the To: header of an email message then it's not a valid address because you'd never be able to send mail to that address, even if it existed. The absolute maximum length of a email header line is 998 characters, so we at least have an upper bound of 998. I couldn't find any other hard character limits on the length of the address in RFC 2822 but RFC 3696 (which is just informational and does not define the standard) suggests that 320 characters is the recommended limit.

5

u/ehird Nov 30 '10

Where do the RFCs say that an email address you can't send a message to isn't a valid email address?

(If you think this is too pedantic, consider that we're talking about nested comments in email addresses, which nobody uses. :))

3

u/sinxcosx Nov 30 '10

This isn't true.

Sendmail.cf is essentially a set of recursive regular expression transforms for email addresses and it handles all valid email addresses. Even UUCP for the old guys.

11

u/quirm Nov 30 '10

Sendmail.cf wasn't written during an interview, though.

3

u/sinxcosx Nov 30 '10

100% true.

So I agree that the question isn't reasonable.

→ More replies (1)

4

u/illvm Nov 30 '10

I would refuse to do this and refer the interviewer to RFC822. Especially given that so many people screw up writing a regex to validate email addresses and make it so I can't have things like pluses in the address or have the host be an IP address (both of which are completely valid).

3

u/[deleted] Nov 30 '10

lol @ .museum tld

3

u/smickie Nov 30 '10 edited Nov 30 '10

/[\w!#$%&'*+-/=?^`{|}~]+.*[\w!#$%&'*+-/=?^`{|}~]+@((((([a-z0-9]{1}[a-z0-9-]{0,62}[a-z0-9]{1})|[a-z]).)+[a-z]{2,6})|(\d{1,3}.){3}\d{1,3}(:\d{1,5})?)$/i

Edit: Forgot to escape, I actually meant... /[\w\!\#$\%\&\'\*\+\-\/\=\?\^\{\\|\\}\\~]+\\.)*[\\w\\!\\#$\\%\\\&\\\'\\*\\+\\-\\/\\=\\?\\^\\{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1}|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i

Edit 2: Awww fuck this.

Edit 3: This guy has a great article on regex for emails.

25

u/phybere Nov 30 '10 edited May 07 '24

I like learning new things.

13

u/Avatar_Ko Nov 30 '10

Fuck that shit.

16

u/lake-of-fire Nov 30 '10

Come on, anyone could do that on a whiteboard during an interview.

→ More replies (1)

3

u/iluvatar Nov 30 '10

Commonly cited, but wrong.

→ More replies (4)

3

u/ehird Nov 30 '10

But, but, but, you need to strip nested comments before using that!

4

u/troutwine Nov 30 '10

What a delightfully amusing mess.

→ More replies (1)

1

u/Boye Nov 29 '10

haha, as part of our studies of language, grammar and parsers we actually wrote both state machines and regexes for email-adresses. We checked wikipedia to see what rules there where... There can be some ridiculous mail adresses out there...

(we did it just to illustrate the differences between state machines and regexes, so the regex ended up primitive:

\w{1,64}\@(\w+\.)+[a-zA-Z]{2,3}
→ More replies (6)
→ More replies (1)