在所有正则表达式中都需要量词吗?

huangapple go评论74阅读模式
英文:

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 = &quot;^[a-zA-Z0-9-]&quot;)
private String subscriberType;
@NotNull @NotEmpty
@Size(min = 32, max = 43)
@Pattern(regexp = &quot;^[a-zA-Z0-9-]&quot;)
private String caseId;
......

I am using regex as below.

@Pattern(regexp = &quot;^[a-zA-Z]&quot;)

If I use above and give input parameters as below,

{
&quot;subscriberType&quot;:&quot;prepaid&quot;,
&quot;caseId&quot;:&quot;5408899645efabf60844de9077372571&quot;
}

I get my validation failed.

Resolving exception from handler [public org.springframework.http.ResponseEntity&lt;java.lang.Object&gt; my.org.presentation.NumberRecycleController.numberRecycle(java.util.Optional&lt;java.lang.String&gt;,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&lt;java.lang.Object&gt; my.org.presentation.NumberRecycleController.numberRecycle(java.util.Optional&lt;java.lang.String&gt;,my.org.domain.request.NumberRecycleReqDto) 
throws java.lang.Exception, with 2 error(s): [Field error in object &#39;numberRecycleReqDto&#39; on field &#39;subscriberType&#39;: 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 &quot;^[a-zA-Z0-9]&quot;]] [Field error in object 
&#39;numberRecycleReqDto&#39; on field &#39;caseId&#39;: 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 &quot;^[a-zA-Z0-9-]&quot;]]

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 = &quot;^[a-zA-Z0-9-]+&quot;

or

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

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.

huangapple
  • 本文由 发表于 2020年8月7日 15:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63296978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定