简体   繁体   中英

Is quantifiers are required in all the Regex expressions?

I am building a Spring Boot application where input parameters are validated using java.validation.*. I want to check my input parameters for alphabetical characters, numbers and hyphen.

public class Foo {
    @NotNull @NotEmpty
    @Pattern(regexp = "^[a-zA-Z0-9-]")
    private String subscriberType;

    @NotNull @NotEmpty
    @Size(min = 32, max = 43)
    @Pattern(regexp = "^[a-zA-Z0-9-]")
    private String caseId;

    ......

I am using regex as below.

@Pattern(regexp = "^[a-zA-Z]")

If I use above and give input parameters as below,

{
    "subscriberType":"prepaid",
    "caseId":"5408899645efabf60844de9077372571"
}

I get my validation failed.

Resolving exception from handler [public org.springframework.http.ResponseEntity<java.lang.Object> my.org.presentation.NumberRecycleController.numberRecycle(java.util.Optional<java.lang.String>,my.org.domain.request.NumberRecycleReqDto) 
throws java.lang.Exception]: org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 1 in method: 
public org.springframework.http.ResponseEntity<java.lang.Object> my.org.presentation.NumberRecycleController.numberRecycle(java.util.Optional<java.lang.String>,my.org.domain.request.NumberRecycleReqDto) 
throws java.lang.Exception, with 2 error(s): [Field error in object 'numberRecycleReqDto' on field 'subscriberType': rejected value [prepaid]; codes [Pattern.numberRecycleReqDto.subscriberType,
Pattern.subscriberType,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: 
codes [numberRecycleReqDto.subscriberType,subscriberType]; arguments []; default message [subscriberType],[Ljavax.validation.constraints.Pattern$Flag;@5dcb2ea8,org.springframework.validation.beanvalidation.SpringValidatorAdapter$ResolvableAttribute@3de9c7b1]; 
default message [must match "^[a-zA-Z0-9]"]] [Field error in object 
'numberRecycleReqDto' on field 'caseId': rejected value [35408899645efabf60844de907737257]; codes [Pattern.numberRecycleReqDto.caseId,Pattern.caseId,Pattern.java.lang.String,Pattern]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [numberRecycleReqDto.caseId,caseId]; arguments []; 
default message [caseId],[Ljavax.validation.constraints.Pattern$Flag;@5dcb2ea8,org.springframework.validation.beanvalidation.SpringValidatorAdapter$ResolvableAttribute@61637994]; default message [must match "^[a-zA-Z0-9-]"]]

I have gone through some similar questions and found a solution. I can get my validation success if I use my regex as below.

@Pattern(regexp = "^[a-zA-Z0-9-]+"

or

@Pattern(regexp = "^[a-zA-Z0-9-]{1,}"

Can you please explain what actually happens here? I know that quantifiers are looking for given number of matches. In my case 1 or more matches which fall into given pattern.

My question is, giving a quantifier is always required or what? What is the reason for the failure of my initial regex pattern?

The pattern must match the whole string. A character class matches only one character. If the string may contain more than one character, you need the quantifier.

Btw. the ^ at the beginning of the regular expression is redundant. The pattern always must match the whole string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM