How to add dot to regex

江枫思渺然 提交于 2021-02-05 08:23:08

问题


@Pattern(regexp="^\\w{8,}")
private String username;

This pattern can only consist of numbers, letters and the underscore character.

How can I add the dot (.) to the pattern

Thanks


回答1:


Try this, it might be easier to understand if you explicitly define all valid characters:

@Pattern(regexp="^[A-Za-z0-9_.]{8,}")
private String username;



回答2:


You need to add end of the line anchor also.

@Pattern(regexp="^[.\\w]{8,}$")


来源:https://stackoverflow.com/questions/29515276/how-to-add-dot-to-regex

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