@Pattern用于字母数字字符串 - Bean验证 [英] @Pattern for alphanumeric string - Bean validation

查看:4237
本文介绍了@Pattern用于字母数字字符串 - Bean验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bean中有一个变量名。我想添加 @Pattern 验证以仅接受字母数字。

I have a variable name in a bean. I want to add @Pattern validation to accept only alphanumeric.

目前,我有这个。

 @NotNull
 @Pattern(regexp = "{A-Za-z0-9}*")
 String name;

但错误是无效的正则表达式。
我试过 [A-Za-z0-9] 。但这也不起作用。虽然没有错误。它将任何有效输入显示为失败。

But the error is Invalid regular expression. I tried [A-Za-z0-9]. But this is not working either. No errors though. It shows any valid input as failed.

推荐答案

您是否尝试此模式: ^ [A-Za -z0-9] * $

^ [A-Za-z0-9] + $ 以避免空结果。

如果要检查字符串是否只包含特定字符,则必须添加锚点( ^ 用于字符串的开头, $ 用于字符串的结尾)以确保您的模式匹配所有字符串。

If you want to check that a string contains only specific characters, you must add anchors (^ for beginning of the string, $ for end of the string) to be sure that your pattern matches all the string.

卷曲括号只是为了写一个数量,例如:我想要两个 a
a {2}
你不能把信件放进去。您可以在大括号中使用unicode字符类的唯一情况是: \p {L} \p {Greek} \p {Arabian} ,...

Curly brackets are only to write a quantity, example: i want two a:
a{2}
You can't put letters inside. The only cases you can meet letters inside curly brackets are when you use unicode character classes: \p{L}, \p{Greek}, \p{Arabian}, ...

这篇关于@Pattern用于字母数字字符串 - Bean验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆