@Pattern for alphanumeric string - Bean validation

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 05:16:49

Do you try this pattern: ^[A-Za-z0-9]*$

or ^[A-Za-z0-9]+$ to avoid empty results.

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.

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}, ...

In addition, you may use a character class, which can be used in curly braces, namely Alnum. For example, for an alphanumeric character having length between 1 and 32 characters inclusive:

@Pattern(regexp = "^[\\p{Alnum}]{1,32}$")

see https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!