Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

383
Visualizações
How do I add regex to @Pattern annotation to match one of two specific strings or a null?

I've added @Pattern annotation to a query parameter in my rest controller (SpringBoot Kotlin). I would like the regex in the pattern to accept -

optionA or optionB or null (nothing/an empty string)

The following works, but of course does not include the empty option -

    @Pattern(regexp = "(?i)optionA||(?i)optionB")

This does not work -

    @Pattern(regexp = "(?i)optionA||(?i)optionB||^\\s*\$")

Can anybody help me with this? :) Thanks!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I tryed this optionA|optionB|^\s$ on https://regex101.com/ and worked well. Could you try on your app to check?

over 4 years ago · Santiago Trujillo Relatório

0

Inside the @Pattern annotation, the pattern is used to match the entire string, so you can use

@Pattern(regexp = "(?i)(?:optionA)?")

which is actually \A(?i)(?:optionA)?\z:

  • \A - start of string (implicit here)
  • (?i) - case insensitive embedded flag option
  • (?:optionA)? - an optional non-capturing group that matches optionA or empty string
  • \z - end of string (implicit here).

The null is a memory address, not a string, it cannot be matched with regex, as regex only deals with strings.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda