简体   繁体   中英

A regular expression to exclude a special character

In Spring MVC For validation use @Pattern annotation like this:

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

I want just exclude & character. @Pattern(regexp = "^&") it's correct ?

^[^&]*?$ 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 &

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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