9

I am trying to modify an existing Regex expression being pulled in from a properties file from a Java program that someone else built.

The current Regex expression used to match an email address is -

RR.emailRegex=^[a-zA-Z0-9_\\.]+@[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$

That matches email addresses such as abc.xyz@example.com, but now some email addresses have dashes in them such as abc-def.xyz@example.com and those are failing the Regex pattern match.

What would my new Regex expression be to add the dash to that regular expression match or is there a better way to represent that?

mattdonders
  • 1,328
  • 1
  • 19
  • 42
  • 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) – dustinroepsch May 03 '16 at 17:36

5 Answers5

8

Basing on the regex you are using, you can add the dash into your character class:

RR.emailRegex=^[a-zA-Z0-9_\\.]+@[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
add
RR.emailRegex=^[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_-]+\\.[a-zA-Z0-9_-]+$

Btw, you can shorten your regex like this:

RR.emailRegex=^[\\w.-]+@[\\w-]+\\.[\\w-]+$

Anyway, I would use Apache EmailValidator instead like this:

if (EmailValidator.getInstance().isValid(email)) ....
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
6

Meaning of - inside a character class is different than used elsewhere. Inside character class - denotes range. e.g. 0-9. If you want to include -, write it in beginning or ending of character class like [-0-9] or [0-9-].

You also don't need to escape . inside character class because it is treated as . literally inside character class.

Your regex can be simplified further. \w denotes [A-Za-z0-9_]. So you can use

^[-\w.]+@[\w]+\.[\w]+$

In Java, this can be written as

^[-\\w.]+@[\\w]+\\.[\\w]+$
rock321987
  • 10,942
  • 1
  • 30
  • 43
2

^[a-zA-Z0-9_\\.\\-]+@[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$

Should solve your problem. In regex you need to escape anything that has meaning in the Regex engine (eg. -, ?, *, etc.).

0

The correct Regex fix is below.

OLD Regex Expression

^[a-zA-Z0-9_\\.]+@[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$

NEW Regex Expression

^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
Rogue
  • 11,105
  • 5
  • 45
  • 71
mattdonders
  • 1,328
  • 1
  • 19
  • 42
  • don't you need to escape the dash when its between square brackets? – Nathan Tregillus May 03 '16 at 17:48
  • I am not 100% sure as Regex is a bit tough for me to understand, but making the change above worked and accepted **abc-def.xyz@example.com** as a valid email address in my program. – mattdonders May 03 '16 at 17:48
  • 1
    That would be because he's accepting any character at all via the unescaped `.` character. This is functionally the same as `.*@.*\\..*` – Rogue May 03 '16 at 18:07
0

Actually I read this post it covers all special cases, so the best one that's work correctly with java is

    String pattern ="(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"; 
Hany Sakr
  • 2,591
  • 28
  • 27