[英]Fluentvalidation 6.4.1.0 support me with Incorrect regex
In my case, i want to validate for url image, some url is valid but result is wrong.在我的例子中,我想验证 url 图像,一些 url 是有效的但结果是错误的。 Eg: link image is "https://fuvitech.online/wpcontent/uploads/2021/02/bta16600brg.jpg" or "https://fuvitech.online/wp-content/uploads/2021/02/bta16-600brg.jpg" reponse "The image link is not in the correct format".
例如:链接图像是“https://fuvitech.online/wpcontent/uploads/2021/02/bta16600brg.jpg”或“https://fuvitech.online/wp-content/uploads/2021/02/bta16-600brg。 jpg”响应“图像链接的格式不正确”。
My code here:我的代码在这里:
RuleFor(product => product.Images)
.Length(1, 3000).WithMessage(Labels.importProduct_ExceedDescription, p => ImportHelpers.GetColumnName(typeof(ProductEntity).GetProperty(nameof(p.Images))))
.Matches(@"^(http:\/\/|https:\/\/){1}?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$").WithMessage(Labels.importProduct_UrlNotCorrect, p => ImportHelpers.GetColumnName(typeof(ProductEntity).GetProperty(nameof(p.Images))));
Please help me where the above regex is wrong.请帮助我上面的正则表达式是错误的。 Thank you.
谢谢你。
Try this:尝试这个:
NOTE the following regex pattern may trigger false positives and also may ignore valid image URLs, because it is very difficult to validate whether a given URL is valid.注意以下正则表达式模式可能会触发误报,也可能会忽略有效的图像 URL,因为很难验证给定的 URL 是否有效。
^https?:\/\/(?:(?:[A-Za-z0-9]+(?:-[A-Za-z0-9]+)+|[A-Za-z0-9]{2,})\.)+[A-Za-z]{2,}(?::\d+)?\/(?:(?:[A-Za-z0-9]+(?:(?:-[A-Za-z0-9]+)+)?\/)+|)[\w-]+\.(?:jpg|jpeg|png)$
Explanation解释
^
the start of a line/string. ^
一行/字符串的开头。
https?:\/\/
match http
with an optional letter s
, followed by ://
. https?:\/\/
将http
与可选字母s
匹配,后跟://
。
(?:(?:[A-Za-z0-9]+(?:-[A-Za-z0-9]+)+|[A-Za-z0-9]{2,})\.)+
This will match things like foo-foo.bar-bar.
(?:(?:[A-Za-z0-9]+(?:-[A-Za-z0-9]+)+|[A-Za-z0-9]{2,})\.)+
这将匹配foo-foo.bar-bar.
, foo.bar-bar.
,
foo.bar-bar.
and foo.
和
foo.
[A-Za-z]{2,}
this will match the TLD part, eg, com
, org
, this part with the previous part will match things like foo-foo.bar-bar.com
, foo.bar-bar.com
or foo.com
. [A-Za-z]{2,}
这将匹配 TLD 部分,例如com
, org
,这部分与前面的部分将匹配诸如foo-foo.bar-bar.com
, foo.bar-bar.com
或foo.com
。
(?::\d+)?
optional group of (a colon :
followed by one or more digits) for port part.端口部分的可选组(冒号
:
后跟一个或多个数字)。
\/(?:(?:[A-Za-z0-9]+(?:(?:-[A-Za-z0-9]+)+)?\/)+|)
this check for two things, the first one is /uploads/public-images/
, /uploads/images/
, the second one is a single /
. \/(?:(?:[A-Za-z0-9]+(?:(?:-[A-Za-z0-9]+)+)?\/)+|)
这个检查两件事,第一个是/uploads/public-images/
, /uploads/images/
,第二个是单个/
。
[\w-]+
this part for the file name, eg, bta16-600brg
. [\w-]+
这部分为文件名,例如bta16-600brg
。
\.(?:jpg|jpeg|png)
you can add here multiple extensions, you can allow uppercase letters by using for example, [Jj][Pp][Gg]
for jpg
. \.(?:jpg|jpeg|png)
您可以在此处添加多个扩展名,您可以允许使用大写字母,例如[Jj][Pp][Gg]
用于jpg
。
$
the end of the line/string. $
行/字符串的结尾。
See regex demo查看正则表达式演示
Thanks @SaSkY answer my question.谢谢@SaSkY 回答我的问题。 I found my mistake.
我发现了我的错误。 This source [ .[az]{2,5}] only allows domain extensions from 2-5 characters.
此来源 [ .[az]{2,5}] 只允许 2-5 个字符的域扩展。 Example [.com] is valid.
示例 [.com] 有效。 But in my case [.online] was not valid.
但就我而言,[.online] 无效。 I changed to [ .[az]{1,10}].
我改为 [ .[az]{1,10}]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.