Spring boot Request Mapping URI Patterns - java

There is a request mapping like this:
#DeleteMapping(value = "/{version:.+}")
I not sure what is the .+ does, but from what i know, this delete mapping can accept a value and match to path variable version, something like:
DELETE
/abc
Value abc will map to path variable version
Why the .+ is needed?
Edited Question:
What is the difference with just /{version}, is there any special case that requires .+?

You can find details or URL matching on this link
URL matching
REGEX: .+ means one or more.
‘*’ Matches 0 or More Characters
'+' Matches 1 or More.

#DeleteMapping(value = "/{version:.+}")
.+ means "one or more of any characters" - thats standard regex/
version: means - put that match in path variable named version.

Related

How do I add regex to #Pattern annotation to match one of two specific strings or a null?

I've added #Pattern annotation to a query parameter in my rest controller (SpringBoot Kotlin). I would like the regex in the pattern to accept -
optionA or optionB or null (nothing/an empty string)
The following works, but of course does not include the empty option -
#Pattern(regexp = "(?i)optionA||(?i)optionB")
This does not work -
#Pattern(regexp = "(?i)optionA||(?i)optionB||^\\s*\$")
Can anybody help me with this? :)
Thanks!
Inside the #Pattern annotation, the pattern is used to match the entire string, so you can use
#Pattern(regexp = "(?i)(?:optionA)?")
which is actually \A(?i)(?:optionA)?\z:
\A - start of string (implicit here)
(?i) - case insensitive embedded flag option
(?:optionA)? - an optional non-capturing group that matches optionA or empty string
\z - end of string (implicit here).
The null is a memory address, not a string, it cannot be matched with regex, as regex only deals with strings.
I tryed this optionA|optionB|^\s$ on https://regex101.com/ and worked well. Could you try on your app to check?

Restrict particular domain in email regular expression

I have an existing regex which validates the email input field.
[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!$%&'*+/=?^_`{|}~-]+)*(\\.)?#(?:[a-zA-Z0-9ÄÖÜäöü](?:[a-zA-Z0-9-_ÄÖÜäöü]*[a-zA-Z0-9_ÄÖÜäöü])?\\.)+[a-zA-Z]{2,}
Now, I want this regex to not match for two particular type of email IDs. Which are wt.com and des.net
To do that I made the following changes in the above expression like this.
[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!$%&'*+/=?^_`{|}~-]+)*(\\.)?#(?!wt\\.com)(?!des\\.net)(?:[a-zA-Z0-9ÄÖÜäöü](?:[a-zA-Z0-9-_ÄÖÜäöü]*[a-zA-Z0-9_ÄÖÜäöü])?\\.)+[a-zA-Z]{2,}
After this it does not matches with any email id which ends with the wt.com and des.net which is right.
But the problem is it does not match with wt.comm or any other letter after the restricted string too..
I just want to restrict email which ends with wt.com and des.net
How do I do that?
Below is the sample emails which should match or not.
ajcom#wt.com : no match
ajcom#aa.an : match
ajcom#wt.coms :match
ajcom#des.net : no match
ajcom#des.neta: match
If you want to prevent only wt.com and des.net which have no characters after it you can add $ anchor (which represents end of string) at the end of each negative-look-ahead.
So instead of (?!wt\\.com)(?!des\\.net) use (?!wt\\.com$)(?!des\\.net$)

neo4j escaping for regular expressions

I receive a user input keyword and want to use it to search my database. I built a query that looks something like this:
db.execute("MATCH (n:User) WHERE n.firstname CONTAINS {keyword} OR n.lastname CONTAINS {keyword} RETURN n.username", params);
But this isn't case sensitive, so I thought of manually building the expression and using regular expressions, sort of as follows:
db.execute("MATCH (n:User) WHERE n.firstname =~ '(?i).*" + keyword + ".*' OR n.lastname =~ '(?i).*" + keyword + ".*' RETURN n.username");
I'm looking either for a function for escaping the regex or a better solution for making the query case-insensitive. Any ideas?
I would suggest storing the properties as all lowercase (or uppercase) and then using the Cypher lower() function to convert user input to lowercase for comparison.
Add lowercase name properties
MATCH (n:User)
SET n.lowerFirstName = lower(n.firstname),
n.lowerLastName = lower(n.lastname)
Find lower case matches based on user input
db.execute("MATCH (n:User) WHERE n.lowerFirstName CONTAINS lower({keyword}) OR n.lowerLastName CONTAINS lower({keyword}) RETURN n.username", params);

Replace querystring attribute value in java using regex

I want to replace querystring value but it's creating some problems:
Problem 1: Its Removing the "&" symbol after replacing
String queryString = "?pid=1&name=Dell&cid=25";
String nQueryString=queryString.replaceAll("(?<=[?&;])pid=.*?($|[&;])","pid=23");
System.out.println(nQueryString);
output of above example
?pid=23name=Dell&cid=25
you can see its removed the "&" after pid
Problem 2: Its not working if I removed the "?" symbol from the queryString variable.
String queryString = "pid=1&name=Dell&cid=25";
String nQueryString=queryString.replaceAll("(?<=[?&;])pid=.*?($|[&;])","pid=23");
System.out.println(nQueryString);
output of above example
?pid=1&name=Dell&cid=25
We can say the regex is not working, So anyone can suggest me better regex which completely fulfill my requirements.
queryString.replaceAll("(?<=[?&;])pid=.*?(?=[&;])", "pid=23")
Difference is that I'm using a positive-lookahead: (?=[&;]), which is zero-length, making it atomic, and is not actually included in the replacement via replaceAll(), just like your original positive-lookbehind is not replaced.
Alternatively, we can match until a & or ; is found, but not included in the replacement, ie:
queryString.replaceAll("(?<=[?&;])pid=[^&;]*", "pid=23")
[^&;] : ^ negates the following: &;, so [^&;]* will match until a ; or & is encountered.
Yours does not work because ($|[&;]) is a non-atomic group, specifically a capturing group, and thus is included in the replacement. NB: a non-capturing group (?:$|[&;]) would also fail here.
To your final note, you're using a positive look-behind for ?, &, and ;, so by removing the ?, it will no longer match, which makes sense.
Use this regex instead:
String nQueryString = queryString.replaceAll("(?<=[?&;])pid=[^&]*", "pid=23");
//=> ?pid=23&name=Dell&cid=25
Here [^&]* is called negation matching pattern, that will match query string value until & is found OR else end of string is found thus leaving rest of the query string un-effected.

How to escape # symbol in url?

I had # symbol to pass as parameter in my URL. But it is discarding all the parameter values after #. Kindly suggest me solution. Following is my url
GetConnectiont?
customerID=customer1&activenode=Sv50&parent=server&frame=imHealthCheckFrame&
connectedFrom=healthCheck&username=root&password=anil#1234&fromAskUsrPwd=askPassword
Thanks in advance
Per the answer found here: How to escape Hash character in URL
Replace # with %23.
You can find a list of all the reserved characters here: Percent-encoding.
A word on JavaScript encoding
encodeURI(str)
"Assumes that the URI is a complete URI, so does not encode reserved characters that have special meaning in the URI. encodeURI replaces all characters except the following with the appropriate UTF-8 escape sequences:" [1]
encodeURIComponent(str)
"Escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )" [2]
In your case, you would use encodeURIComponent(), only on your query string, to escape any and all reserved chracters.
! %21
# %23
$ %24
& %26
' %27
( %28
) %29
* %2A
+ %2B
, %2C
/ %2F
: %3A
; %3B
= %3D
? %3F
# %40
[ %5B
] %5D
You would need to encode the parameters that may contain # and then decode back the parameter in server side code.
You can use many resources in the internet to URL-encode strings, like this. In your case, anil#1234 becomes anil%231234.
You need to encode the value(if you are using string concatenation to create the url). in javascript you can use encodeURIComponent() like
encodeURIComponent('anil#1234');//or your variable here

Categories

Resources