0

Right now I have:

(?!.*([._-])\1)(?=.*@)[\w.@-]+

which finds test@foo

I want to make it so that test cannot start or end with a special character.

For instance, I want it to find:

  • tes-t@foo
  • test@-foo

but not:

  • -test@foo
  • test-@foo
  • -test-@foo
moinudin
  • 134,091
  • 45
  • 190
  • 216
bjacobs
  • 401
  • 1
  • 6
  • 17
  • Note that using regular expressions to match email addresses is a bad idea; you cannot do it exactly, and 'coming close' will reject valid emails. (I know, I happen to use an email of `!@mydomain.net` and a surprising number of sites reject it, despite it being [RFC 2822 valid](http://www.faqs.org/rfcs/rfc2822.html).) If you insist on doing so, read [the work of others](http://www.regular-expressions.info/email.html) who have already gone deep on this topic. – Phrogz Jan 19 '11 at 20:38
  • 3
    What have you tried so far? It seems to me like you're just saying "I want a regex to match this" without actually trying to either modify or learn regexes. All of your questions are of the form "I have this regex and I want it to match this, please tell me how to change it" without any indication that you're actually learning anything about regular expressions along the way. Please read a tutorial (such as http://www.regular-expressions.info/) and try to fix your regexes to do what you want before blindly coming here first! – CanSpice Jan 19 '11 at 20:40
  • I can't match "I want to make it so that test cannot start or end with a special character" to ".. but not 'test-@foo'" - it does not start with special char, and does not end with such... – Maxym Jan 19 '11 at 20:46
  • @Maxym `test-` ends with a dash, which is apparently a "special character". – CanSpice Jan 19 '11 at 20:48
  • Define ‘special character’, please. – tchrist Jan 19 '11 at 21:11
  • 1
    @Phrogz: What do you mean, cannot do it exactly? [Sure you can!](http://stackoverflow.com/questions/764247/why-are-regular-expressions-so-controversial/4053506#4053506). And yes, that RFC 5322 regex indeed passes your silly address, as it is compliant albeit ill-advised. – tchrist Jan 19 '11 at 21:15
  • @tchrist RFCs 2822 and 5322 allow arbitrarily-deep nested comments. However unlikely they may be, this prevents a single regular expression for validating every legal address. See for example, [this answer](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/1044515#1044515) which only matches nested comments up to four levels deep. Yes, my "ill-advised" email address is trivially matched by a regexp written by someone with more knowledge of legal addresses than the asker of this question. – Phrogz Jan 19 '11 at 21:25
  • @Phrogz: Your statement is incorrect. If you look at my (well, Abigail’s, and thence Damian’s) [RFC 5322–validating pattern](http://stackoverflow.com/questions/764247/why-are-regular-expressions-so-controversial/4053506#4053506), you will see that it handles arbitrarily nested comments perfectly well. For example, `this (deep (and other (stuff) here (that goes (more levels (deep (in) it) is) hard to) get) right) stuff ` validates with no trouble. – tchrist Jan 19 '11 at 21:37
  • @Phrogz: Good day to leave work early here in Boulder. :) – tchrist Jan 19 '11 at 21:40
  • @tchrist No no, good day to drive up to Ned and see if I'll need to use the new winch or not. :) I will have to examine your/others' answer more deeply. _edit: Ohhh...sure, for crazy non-regular expressions in Perl is possible. :)_ – Phrogz Jan 19 '11 at 21:58
  • @tchrist And to be clear, that is a marvelous answer and excellent example of BNF-style-grammar-matching. It's just not applicable for "regular expressions" as a language, or specifically (for this question) JavaScript. – Phrogz Jan 19 '11 at 22:09
  • @Phrogz I didn’t see a javascript tag on the question. Javascript’s patterns are always troublesome due to lack of Unicode support. Once you get hooked on separating declaration from execution, it’s hard to go back to primative greppy things nobody can ever maintain. I do expect to see more stuff pick up PCRE; php already has. I thought we weren’t supposed to *get* Real Weather out of this system, but hey never trust a weatherman. – tchrist Jan 19 '11 at 22:50
  • @tchrist Uh...(rushes to add [javascript] tag). Not sure why I thought this was a JS question. *blush* – Phrogz Jan 19 '11 at 23:15

2 Answers2

0

You can define a character class that doesn't include specific characters using ^, e.g. [^a] will match anything apart from an a.

I would split the regex that matches the pre-@ word into three sections; one to match the leading character, one to match the middle, and one to match the last character. You'll also need to handle the special case of the pre-@ word only having a single character.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

This is not an area where you should be recreating the wheel: there’s too much to get wrong.

I’m not sure what you really want to do. Addresses like president@whitehouse.gov and a plain old postmaster are probably both deliverable but highly unlikely to do what you want.

The only reasonable way to validate a mail address is to send mail to that address and get back a non-automatable reply showing that it is the right human at the other end. But this cannot be done in real time. Which means the best you can do is make them type it twice to try to weed out typos. That’s not much help, really. That’s why signing up for something over the web always involves a negotiated handshake.

However, if all you need is to validate an RFC 5322–compliant address, you may use this pattern.

Just understand that testing an address for compliance with the RFC should never be confused with validating that mail address — which is something else altogether.

Community
  • 1
  • 1
tchrist
  • 78,834
  • 30
  • 123
  • 180