我需要在輸入值應以“Type”結尾的地方使用驗證所以我想出了Type$正則運算式,但它對于正確的值失敗了。這是我在代碼中的模式注釋中使用它的方式
@Pattern(regexp = REGISTRY_CONFIG_TYPE_FORMAT,message = REGISTRY_CONFIG_TYPE_ERROR)
@ApiModelProperty(name = "registryConfigType", dataType = "String", value = "OccasionType", example = "OccasionType", required = true)
private String registryConfigType;
常數值
REGISTRY_CONFIG_TYPE_FORMAT = "Type$"
當我傳遞諸如 OccasionType 之類的值時,我收到了錯誤訊息。但在 regex101 上它作業正常。不確定問題出在哪里。

以下是我得到的錯誤日志
Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public com.macys.registry.dataobject.v1.response.RegistryAppResponse<java.lang.Object> com.macys.registry.controller.RegistryConfigController.updateConfig(com.macys.registry.dataobject.v1.request.UpdateRegistryConfigRequest): [Field error in object 'updateRegistryConfigRequest' on field 'registryConfigType': rejected value [OccasionType]; codes [Pattern.updateRegistryConfigRequest.registryConfigType,Pattern.registryConfigType,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [updateRegistryConfigRequest.registryConfigType,registryConfigType]; arguments []; default message [registryConfigType],[Ljavax.validation.constraints.Pattern$Flag;@20999237,Type$]; default message [Registry Config Type Should Not Be Null And Should End With [Type]]] ]
uj5u.com熱心網友回復:
試試這個
REGISTRY_CONFIG_TYPE_FORMAT= ".*Type$";
uj5u.com熱心網友回復:
從 javadoc 中可能不是 100% 清楚,但regexp必須匹配. 換句話說,整個輸入必須由regexp. 一個簡單的修復:.*Type. 請注意,這$是不必要的,因為匹配已經暗示了這一點。
如果find where used 而不是match ,您的正則運算式將是有效的。
uj5u.com熱心網友回復:
嘗試將您的常量更改為:
REGISTRY_CONFIG_TYPE_FORMAT = "\\w*Type\\b";
從RegExr:
\w Word. Matches any word character (alphanumeric & underscore).
* Quantifier. Match 0 or more of the preceding token.
T Character. Matches a "T" character (char code 84). Case sensitive.
y Character. Matches a "y" character (char code 84). Case sensitive.
p Character. Matches a "p" character (char code 84). Case sensitive.
e Character. Matches a "e" character (char code 84). Case sensitive.
\b Word boundary. Matches a word boundary position between a word character and non-word character or position (start / end of string).
希望這會有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/515236.html
標籤:爪哇正则表达式春天休息
