简体   繁体   English

如何在Java中使用正则表达式允许空字符串?

[英]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);
if(!UMORN)
    PopMyError();

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 * : 最简单的解决方案是将量词更改为*

"[a-zA-Z0-9-~!_#(@).]*"
                     ^ 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. @TheLostMind建议使用*效率低下,在使用正则表达式检查有效字符之前,应先检查null和empty输入。

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-~!_#(@).]+")) {
    PopMyError();
  }
}

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-~!_#(@).]*" . 这与说“空用户名有效”(由正则表达式"[a-zA-Z0-9-~!_#(@).]*"传达"[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). (当然,我们可以通过预编译模式以创建Pattern对象,然后在条件中调用pattern.matcher(UserName).matches()来提高效率)。

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; 我猜想上述逻辑将在用户仍在输入数据的某些UI代码中使用; 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. 最好重用尽可能多的逻辑,以免出现不一致的情况:使用+形式的正则表达式将适合于此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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