jacky jacky - 1 month ago 4x
Java Question

how to allow empty String using regex in java?

Basically i have an email box where it only should contain characters are allowed in email boxes but can be empty, the problem is that when the box is empty then it pops my error which i have created saying

Email box contain character that are not allowed

String UcharSet = "[a-zA-Z0-9-~!_#(@).]+";
boolean UMORN = UserName.matches(UcharSet);

as long as i know i don't have the null character in my allowed list, so can you tell me how to allow the box can be empty too?

and do tell have i got all characters that are allowed in Emails.


The simplest solution is to change the quantifier to *:

                     ^ zero or more

You are currently using +, which requires one or more matches.

@TheLostMind has suggested that using * is inefficient, and you should check for null and empty input first, before using the regex to check for valid characters.

Whilst I agree that it would be more efficient to do so, I think that efficiency is a secondary reason here: if your intent in the code is to check the contents of the string only if the user has entered it, it is better to structure it in that way:

if (UserName != null && !UserName.isEmpty()) {  // Or use your favorite library.
  // The user has entered a value: check that it is valid.
  if (!UserName.matches("[a-zA-Z0-9-~!_#(@).]+")) {

which is subtly different from saying that "an empty user name is valid", which is what is conveyed by the regex "[a-zA-Z0-9-~!_#(@).]*".

(Of course, we can make this more efficient by precompiling the pattern to make a Pattern object, and then calling pattern.matcher(UserName).matches() in the condition).

Another advantage of pre-checking for empty/null strings is that you can reuse the same regular expression for validating in other parts of your code, where you don't want to allow such user names.

I'm guessing the above logic is to be used in some UI code, where the user is still entering the data; you might want to ensure that the username really is valid before you actually commit the data to the database, though. There, you'd not want to allow empty usernames; it is better to reuse as much logic as possible, to avoid inconsistencies: having the + form of the regex would be appropriate there.