title: How to allow specific words using javax.validation.constraints.Pattern?
body: <p>I would like to use javax.validation.constraints.Pattern for validation. The text should not be none or others. </p>
  
  <p>Allowed Examples:</p>
  
  <ul>
  <li>something</li>
  <li>word</li>
  </ul>
  
  <p>NOT allowed:</p>
  
  <ul>
  <li>none</li>
  <li>others</li>
  </ul>
  
  <p>I am trying around but I dont catch my issue. Something like:</p>
  
  <pre><code>@NotNull
  @Pattern(regexp = "(^none)")
  String  countryValue;
  </code></pre>
  
  <p>Thanks for your hint.</p>
  
  <p><strong>UPDATE:</strong>
  As Anish said with the online regex validator, the regex <code>^(?!others|none)</code> should be correct. But Spring-MVC still denied. Is there a special syntax to be used? I give more code to have a bigger picture:</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>@PostMapping
  public String post(@ModelAttribute @Valid DisclaimerFormDto disclaimerForm, BindingResult errors, ModelMap modelMap) {
      if(errors.hasErrors()) {
          errors.getAllErrors().forEach(System.out::println);
          return "redirect:/disclaimer";
      }
      return "redirect:/product";
  }
  </code></pre>
  
  <p><strong>FormDto (with changes mentioned from Anish):</strong></p>
  
  <pre><code>@Data
  @ToString
  public class DisclaimerFormDto {
      @NotNull
      @Pattern(regexp = "^(?!others|none)")
      String  countryValue;
  }
  </code></pre>
  
  <p><strong>Output of BindingResult:</strong></p>
  
  <pre><code>Field error in object 'disclaimerFormDto' on field 'countryValue': rejected value [none]; codes [Pattern.disclaimerFormDto.countryValue,Pattern.countryValue,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [disclaimerFormDto.countryValue,countryValue]; arguments []; default message [countryValue],[Ljavax.validation.constraints.Pattern$Flag;@59374db6,^(?!(none|others)$).*$]; default message [muss auf Ausdruck "^(?!(none|others)$).*$" passen]
  </code></pre>