Grouping with OR in regex

The below is my regex pattern. I am using this to validate an email address.

^[\\w]+(\\.|\\_)?[\\w]+\\@{1}[\\w]+\\.{1}(([A-Za-z]+)|(\\.{1}[A-Za-z]+))$

The email ID must follow the following rules . 1. Contains any number of alphabets or numbers before 1 period or underscore. 2. Followed by any number of alphabets or numbers before 1 @. 3. Followed by any number of alphabets or numbers before 1 period. 4. (Followed by any number of alphabets) or (1 period and any number of alphabets).

I am facing an issue with the 4th rule. It works fine for a email ID ending with @xyz.abc, but fails for @xyz.abc.ab

Is it not possible to group patterns for the fourth rule as I have done ?

Solution :

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

Answers


Based on your description, what you want is actually:

^[A-Za-z0-9]+[\\._][A-Za-z0-9]+@[A-Za-z0-9]+(?:\\.[A-Za-z]+)+$

Splitting this out, you have:

  1. Alphas or numbers ([A-Za-z0-9]+)
  2. Underscore or period ([\\._])
  3. Alphas or numbers ([A-Za-z0-9]+)
  4. At sign (@)
  5. Alphas or numbers ([A-Za-z0-9]+)
  6. One or more of period plus alphas ((?:\\.[A-Za-z]+)+)

Note that \\w contains _, so you can't use it here.


Need Your Help

Server Wide Variables in Project Deployment (2012 SSIS)

sql-server ssis sql-server-2012 sql-server-2014 ssis-2012

I've read about the use of Catalogs in 2012/14 SSIS as a replacement for Configurations in 2008. With that replacement, I haven't seen how people handled the scanario of a configuration that is us...