简体   繁体   English

排除特殊字符的正则表达式

[英]A regular expression to exclude a special character

In Spring MVC For validation use @Pattern annotation like this: 在Spring MVC中,使用@Pattern批注进行验证,如下所示:

@Pattern(regexp = "???", message = "#i18n{obligatoire}")
@NotEmpty   
private String stringTest;

I want just exclude & character. 我只想排除&字符。 @Pattern(regexp = "^&") it's correct ? @Pattern(regexp = "^&")是正确的吗?

^[^&]*?$ is regex you are looking for. ^[^&]*?$是您要查找的正则表达式

@Pattern(regexp = "^[^&]*$")
@NotEmpty   
private String stringTest;

Explanation: 说明:

  • [^&] captures any character that is not & [^&]捕获任何非&字符
  • [^&]* captures all characters that are not & [^&]*捕获所有非&字符
  • ^[^&]*$ captures all characters on the line ( ^ is the beggining of the line, $ is the end of the line) that are not & ^[^&]*$捕获行中不是&所有字符( ^是行的开始, $是行的结尾)

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

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