Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

380
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda