简体   繁体   中英

Email Regex to allow only 1 character aposthrope before @symbol

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM