-1

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:

In the database I get the rule for URI: /uaa/api/users/*/city/*

Replace the '*' with the regex so that it accepts any character between / or after /: ^/uaa/api/users/+(.*)+/city/+(.*)$

I compare the URI with regex with the request 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?

I do not know if that matters, but I'm using regex in Java!

user2831852
  • 535
  • 2
  • 7
  • 22

2 Answers2

1

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. This part (.*)+ also uses that quantifier to repeat a group that itself matches 0+ times any character (inclusing the forward slash).

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.

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

See the regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

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
Simon
  • 925
  • 3
  • 13
  • 30