Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a class with field begDate to be validated:

public class Booking
    @Type(type = "uuid-char")
    private UUID   uuid;

    // there are many more fields and nothing more, the most of them aren't to validate and don't have annotations
    @Pattern(regexp = "^(0$|[^0]\\d{0,19}$)", flags = Pattern.Flag.MULTILINE)
    @Length(min = 1, max = 20)
    private String begDate;


The idea is to match either line, consisting of only one zero or line, consisting of 0 or more numbers NOT beginning with zero. I've tested this expression here, and as you can see it works — the lines, beginning with zero don't match.

However, calling

validator.validateProperty(obj, "begDate")

on an object, containing begDate equal to '09980514122516231246' (the lowest line in the linked example)

@Pattern(regexp = "^(0$|[^0]\d{0,19}$)", flags = Pattern.Flag.MULTILINE)
@Length(min = 1, max = 20)
private String begDate;

doesn't return any error. Nonetheless, the simpler expressions work as expected — validation of the following property

@Pattern(regexp = "[0-9]{13} {4}")
@Length(min = 17, max = 17)
private String orderNr;

set to 'sdf5555555550' will return a set of ConstraintViolations, consisting of one element (as expected).

I am a bit confused, because understands my regex (I also tried all flavors). Are there any limitations on regular expression inside @Pattern or maybe jakarta uses some specific dialect?

For further clarity, here are the strings, that should validate:





And the strings, that shouldn't:




Now all of the lines above are successfully validated. Important: I always get only one line of string without any carriage returns, every new value of begDate gets validated in a new iteration.

What I have tried:

I tried to play with regexp itself, adding/removing $ at the end; tried it with and without MULTILINE flag.
Updated 6-Feb-22 23:25pm

Try this one:
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre -[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900