Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

378
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!