I have a class with field
begDate
to be validated:
@Data
@Entity
public class Booking
{
@Id
@GeneratedValue
@Type(type = "uuid-char")
private UUID uuid;
@NotEmpty
@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
@NotEmpty
@Pattern(regexp = "[0-9]{13} {4}")
@Length(min = 17, max = 17)
private String orderNr;
set to 'sdf5555555550' will return a set of
ConstraintViolation
s, consisting of one element (as expected).
I am a bit confused, because
regex101.com 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:
19980514122516231246
19990514122516231246
19980524561516231246
0
And the strings, that shouldn't:
09980514122516231246
00980514122516231246
00
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.