-3

I have a JAVA string that is being used to validate proper email addresses.

^[\\w'-]+(\\.[\\w'-]+)*@[A-Za-z0-9]+([.-][A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

I want to user to be able to leave the address blank and not get an error message. How is this possible?

koddos
  • 1
  • 2
  • Just make a condition for that. Its easier than reconfiguring the whole regular expression. – Havenard May 20 '13 at 19:24
  • What programming language are you using? There may be a better solution than regex. Email addresses aren't easy to parse using a regex, but many languages have fairly standard functions for validating email addresses. For example, in PHP, you can simply use `filter_var()`; no regex required. – Spudley May 20 '13 at 19:30

2 Answers2

0

Since you didn't provide any other code or specify what language you were using, the best I can suggest is:

(?:^[\\w'-]+(\\.[\\w'-]+)*@[A-Za-z0-9]+([.-][A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$)|(?:^$)

Which will match an e-mail or an empty string.

qJake
  • 16,821
  • 17
  • 83
  • 135
0

If you have an expression you want to make optional allowing empty string to match you could use any of the following:

^(?:regex)?\z
^\z|^regex\z
Qtax
  • 33,241
  • 9
  • 83
  • 121