-4

How can I modify this regex for email? Current my regex does not allow for an apostrophe:

"^[A-Za-z0-9_\\-\\.]+[@]([A-Za-z0-9\\-\\.]+)+[\\.]([A-Za-z]{2,4})$";

Now I want add apostrophe, however the request is to only allowed 1 apostrophe before @ symbol.

I tried to use this:

"^([A-Za-z0-9_\\-\\.]+[']{0,1})+[@]([A-Za-z0-9\\-\\.]+)+[\\.]([A-Za-z]{2,4})$";

It allows apostrophe input, however I can input more than 1 apostrophe before @ symbol

Result:

test''test@yahoo.com -> not allowed

test'tes't@yahoo.com -> allowed (expected not allowed)

Expected result is that only one apostrophe is allowed before @ symbol.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • 2
    possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – Qantas 94 Heavy Mar 31 '14 at 11:52
  • 1
    possible failure to understand email syntax :) – n00b Mar 31 '14 at 11:53

1 Answers1

0

here is a modified version of your regex :

"^([A-Za-z0-9_\\-\\.]*?)'?([A-Za-z0-9_\\-\\.]+)[@]([A-Za-z0-9\\-\\.]+)+[\\.]([A-Za-z]{2,4})$";

the way it works is - it allows OPTIONAL characters, then an optional apostrophe, then non-optional characters.

only downside is you cannot have a ' right before @ (also at least 2 characters after apostrophe) this can be tweaked more but i dont really see any point at all

n00b
  • 5,642
  • 2
  • 30
  • 48
  • Is this a modified regex or another regex entirely? If it's just been modified slightly, it'd be good if you could include what you have changed. – Qantas 94 Heavy Mar 31 '14 at 11:51
  • it is a modified regex. it has optional characters + optional apostrophe + characters. only downside is you cannot have a ' right before @ (also at least 2 characters after apostrophe) this can be tweaked more but i dont really see any point at all – n00b Mar 31 '14 at 11:51
  • Yeah, might be a good idea to put that in your answer to make it more clear. – Qantas 94 Heavy Mar 31 '14 at 11:57