英文:
Is quantifiers are required in all the Regex expressions?
问题
我正在构建一个Spring Boot应用程序,其中使用java.validation.*来验证输入参数。我想检查我的输入参数是否包含字母字符、数字和连字符。
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;
// ......
}
我正在使用以下正则表达式。
@Pattern(regexp = "^[a-zA-Z]")
如果我使用上面的正则表达式并提供以下输入参数:
{
"subscriberType":"prepaid",
"caseId":"5408899645efabf60844de9077372571"
}
我得到了验证失败的结果。
Resolving exception from handler [public org.springframework.http.ResponseEntity<Object> my.org.presentation.NumberRecycleController.numberRecycle(Optional<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<Object> my.org.presentation.NumberRecycleController.numberRecycle(Optional<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-]"]]
我查看了一些类似的问题并找到了解决方案。如果我将正则表达式更改为以下形式,就可以成功验证。
@Pattern(regexp = "^[a-zA-Z0-9-]+")
或者
@Pattern(regexp = "^[a-zA-Z0-9-]{1,}")
你能否解释这里到底发生了什么?我知道量词用于寻找给定模式的匹配次数。在我的情况下,是1个或更多个匹配项符合给定的模式。
我的问题是,是否总是需要使用量词,或者为什么我的初始正则表达式模式失败了的原因是什么?
英文:
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?
答案1
得分: 3
模式必须完全匹配字符串。字符类只匹配一个字符。如果字符串可能包含多个字符,则需要使用量词。
顺便说一下,正则表达式开头的 ^
是多余的。模式始终必须完全匹配整个字符串。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论