简体   繁体   English

正则表达式以验证URI中的参数

[英]Regex to validate parameters in the URI

I'm trying to validate URIs of the system with regex to identify parameters in the middle of the URI of a REST API, however, I'm having problems with parameter validation at the end of the URI, for example: 我正在尝试使用正则表达式验证系统的URI,以在REST API的URI中间标识参数,但是,在URI的末尾遇到参数验证问题,例如:

In the database I get the rule for URI: /uaa/api/users/*/city/* 在数据库中,我获得了URI的规则: /uaa/api/users/*/city/*

Replace the '*' with the regex so that it accepts any character between / or after /: ^/uaa/api/users/+(.*)+/city/+(.*)$ 用正则表达式替换“ *”,以便它接受/或/之后的任何字符: ^/uaa/api/users/+(.*)+/city/+(.*)$

I compare the URI with regex with the request URI: /uaa/api/users/john.connor/city/12 我将带有正则表达式的URI与请求URI进行比较: /uaa/api/users/john.connor/city/12

But if I remove the value at the end of the URI and leave only the / it accepts being that I need it to fail because there is no value after /, that is, when using the ^/uaa/api/users/+(.*)+/city/+(.*)$ regex with the /uaa/api/users/john.connor/city/ URI I need it to fail because there is no value after /.How can I do it so he does not accept a worthless bar followed in the end? 但是,如果我删除URI末尾的值,而只保留/,则表示接受它需要我失败,因为/后面没有值,也就是说,使用^/uaa/api/users/+(.*)+/city/+(.*)$带有/uaa/api/users/john.connor/city/ URI的正则表达式我需要它失败,因为/之后没有值。我该怎么做,所以他最终不接受毫无价值的酒吧吗?

I do not know if that matters, but I'm using regex in Java! 我不知道这是否重要,但是我在Java中使用正则表达式!

There are a few things to note about the pattern that you are using. 关于您使用的模式,需要注意几件事。

The plus in /+ is a quantifier in this context matching 1+ times a forward slash. 在这种情况下, /+的加号是一个量词,与正斜杠匹配1+倍。 This part (.*)+ also uses that quantifier to repeat a group that itself matches 0+ times any character (inclusing the forward slash). 这部分(.*)+还使用该量词来重复一个本身与任何字符匹配0+次的组(包括正斜杠)。

I think what you are looking for is to match not a forward slash using a negated character class [^/]+ between the forward slashes and for the last part as well so that the url can not end on a forward slash. 我认为您正在寻找的是在正斜杠之间以及最后一部分之间使用否定的字符类 [^/]+而不是正斜杠,以使URL不能以正斜杠结尾。

^/uaa/api/users/[^/]+/city/[^/]+$

See the regex demo 正则表达式演示

Here 这里

This contains at least one character from each of the following categories. 它至少包含以下每个类别中的一个字符。

Lowercase character Uppercase character Digit Symbol 小写字符大写字符数字符号

(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)

(?=.*[a-z])        // use positive look ahead to see if at least one lower case letter exists
(?=.*[A-Z])        // use positive look ahead to see if at least one upper case letter exists
(?=.*\d)           // use positive look ahead to see if at least one digit exists
(?=.*\W])        // use positive look ahead to see if at least one non-word character exists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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